Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
Go Back  Xtreme Visual Basic Talk > > > Creating a random new point within a picturebox bounds


Reply
 
Thread Tools Display Modes
  #1  
Old 06-18-2011, 12:08 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default Creating a random new point within a picturebox bounds


How do I go about doing this? I have currently made this code, but sometimes points are created on the boundaries of this picture box, which I do not want. I want all new random points to be within the bounds of this large playarea (picturebox). Foodarea is my big picturebox which i want all fruit to spawn WITHIN not outside on it's edges.

Code:
        Dim ax As Integer = CInt(Rnd() * (Foodarea.Width - 35)) ' sets the random area of the food
        Dim ay As Integer = CInt(Rnd() * (Foodarea.Height - 35))

        Apple.Location = New Point(ax, ay)
I -35, because my biggest fruit size is 30*30 sized picture box. so i thought subtracting -35 would make it not go on the edges of foodarea, however it still does.
thanks.
Reply With Quote
  #2  
Old 06-18-2011, 11:44 PM
passel's Avatar
passelCreating a random new point within a picturebox bounds passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

Add an offset. You're generating a value from 0 to Foodarea.Width - 35
You probably want the 35 border on both left and right, bottom and top.
so set ax to 35 + (Rnd() * (Foodarea.Width - 70)
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 06-19-2011, 01:48 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by passel View Post
Add an offset. You're generating a value from 0 to Foodarea.Width - 35
You probably want the 35 border on both left and right, bottom and top.
so set ax to 35 + (Rnd() * (Foodarea.Width - 70)
hey, yeah I was just wondering, does this do the same thing? that is, set an off set: 31 and 26 being the offsets. so it's a random point within 898*322 at location (31,26) - this being the upper left corner of the offset.

Code:
 Dim ax As Integer = CInt(Rnd() * 898 + 31) ' sets the random area of fruit 
Dim ay As Integer = CInt(Rnd() * 322 + 26)
Reply With Quote
  #4  
Old 06-19-2011, 10:34 AM
OnErr0r's Avatar
OnErr0rCreating a random new point within a picturebox bounds OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Use the ClientSize.Width/Height for your inner picturebox dimensions. Use the width/height of the fruit picturebox for subtraction. Never hardcode numbers.

You should also be using the Random class for random number generation, not Rnd.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #5  
Old 06-28-2011, 08:00 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by OnErr0r View Post
Use the ClientSize.Width/Height for your inner picturebox dimensions. Use the width/height of the fruit picturebox for subtraction. Never hardcode numbers.

You should also be using the Random class for random number generation, not Rnd.
why never hard code numbers? I have done it initially for one of my forms and the randomisation of points works fine. and it's hardcoded with numbers. Just curious why I shouldn't use it? And what's random class compared to Rnd()
Reply With Quote
  #6  
Old 06-28-2011, 09:47 PM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

When you make your form larger, there will be all those constants that need to be changed.
Reply With Quote
  #7  
Old 06-28-2011, 10:06 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by 45minutes View Post
why never hard code numbers? I have done it initially for one of my forms and the randomisation of points works fine. and it's hardcoded with numbers. Just curious why I shouldn't use it? And what's random class compared to Rnd()
Fixed position is not a bad place to start, providing yours is the only screen it is going to be used on, but when you want to make an application that can be used across multiple devices you need to pay more attention to making all your objects relative.

By using things like screen height and screen width you can scale all the object on your form acordingly.

For example lets say you make a phone dialer that has 10 buttons on it that takes up a quarter of the screen, if you don't disable the maximise option, when a user clicks full screen you may wind up with all 10 buttons in the top left hand corner.

You could make all the buttons larger to fill the space or you could simply move the buttons to the centre but work with relative positioning you can make sure you never get caught out with a button missing from the screen.
Reply With Quote
  #8  
Old 06-29-2011, 03:51 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by Rockoon View Post
When you make your form larger, there will be all those constants that need to be changed.
I made my forms un-sizeable.
Reply With Quote
  #9  
Old 06-29-2011, 04:14 AM
PlausiblyDamp's Avatar
PlausiblyDampCreating a random new point within a picturebox bounds PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Quote:
Originally Posted by 45minutes View Post
I made my forms un-sizeable.
How about if you need to change the size you fix the forms at though? If you need to target larger / small monitors or different form factors you will need to go and adjust all the hard coded values when you change the form sizes.

If the values are based on the form size then if you change the size in the designer nothing more needs to be done.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #10  
Old 06-29-2011, 08:51 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by PlausiblyDamp View Post
How about if you need to change the size you fix the forms at though? If you need to target larger / small monitors or different form factors you will need to go and adjust all the hard coded values when you change the form sizes.

If the values are based on the form size then if you change the size in the designer nothing more needs to be done.
That's the thing. My program wasn't designed to be fit other monitors. Most of the forms are small enough though to fit on any given screen today. There's the exception of 2 forms though which are pretty big and just barely fit on my screen here at home. Guess it's kinda un-ergonomical, but hey it's my first version I'm learning maybe version 1.1 will feature sizable forms with that non-hard-coded stuff you're talking about.

btw another thing, would you know how to move a form across a screen using a picturebox. Because I got rid of the windows forms boarder and selected "none" for the option. I made my own skin, but I do not know the code to let the API move my form across the screen when the mouse clicks the top taskbar thingy - what's the proper term called? any ways one of my mates said it was 1 line of code, but I think it's much more involved because I remember researching about it and it looked complex to do.
Reply With Quote
  #11  
Old 06-29-2011, 09:01 AM
DrPunk's Avatar
DrPunkCreating a random new point within a picturebox bounds DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Why do you need a picturebox? Just set the BackgroundImage property of the form instead.
__________________
There are no computers in heaven!
Reply With Quote
  #12  
Old 06-29-2011, 11:00 AM
AtmaWeapon's Avatar
AtmaWeaponCreating a random new point within a picturebox bounds AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

With respect to the original discussion, I prefer using the Random class to get a random number. It's usually best to use the .NET API for something rather than the VB6 API. In this case, Rnd() requires you to understand the formula to get a random number between two numbers min and max:
Code:
range = max - min
value = min + (Rnd() * range)
.NET only requires you to ask for a number between the range:
Code:
' Assume RNG is some instance of the Random class:
value = RNG.Next(min, max)
With respect to the discussion about hard-coding numbers, it's a bad practice. There are several reasons.

Hard-coded numbers have a bad habit of propagating throughout weird parts of the program. You might understand your picture box is always 100px wide, so you just type out 100 every time you need the width. In 5 months, you decide it's a little small so you adjust it to 110. Find/replace 100 to 110 right? Wrong; maybe you decided some other element was 100px wide too. Find/Replace is going to break code that depends on the *other* hard-coded 100 values. You'll have to do a Find and look at each instance one by one to decide if it needs to change. If you were using the control width or constants, you could make one change and be done.

Hard-coded numbers are "magic numbers" that become unreadable over time. One day, you're going to look at an equation like this and have no idea what's going on:
Code:
TextBox2.Location = 1080 - 100 - 5 - 200 - 5
It'd be much more clear if you used symbolic values rather than magic numbers:
Code:
TextBox2.Location = ClientRectangle.Width - borderImage.Width - buffer - adBanner.Width - buffer
Even if you don't use the control variables, replacing magic numbers with constants has a dramatic impact on the readability of your code.

I understand your argument in #10 that you're just learning: this is the best time to get in the habit of using the size of controls rather than magic numbers. The longer you wait before you learn the practice, the harder it will be to figure out when you need it.

So you want to enable moving the form in a borderless form? Your mate's only half right. It's only one line of code if you ignore the 10 or 15 lines of infrastructure. Here's the long way in C#. Don't whine that you can't read C#, it's not the way you should be implementing it anyway. This is the short way in C#. Normally I tell people to convert it themselves, but it's sort of tricky. Probably about as hard to find an implementation online in VB .NET. It's hard as heck to find good examples in VB .NET; do yourself a favor and learn C#.

Actually that snippet sucks, so I've attached a project that does it myself. It's got a label that serves as a fake title bar that can be used to drag the form. Comments explain how it works.
Attached Files
File Type: zip MouseDragDemo.zip (14.9 KB, 2 views)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #13  
Old 06-29-2011, 04:07 PM
passel's Avatar
passelCreating a random new point within a picturebox bounds passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

The random class is also better in general compared to the older Rnd function. It has a larger sequence before repetition. The Rnd function sequence repeats after 16 million or so (2^24), but the seed is reduced to 2 byte value so the number of unique starting points for the sequences is only 65536.

Another fairly simple way to move a form when dragging on the form itself (or another control that allows mouse inputs) I've used for years is to take advantage of the Static variables allowed in VB subs and add this bit of code to the MouseMove event handler.
Code:
    Static lx, ly As Integer
    If e.Button = Windows.Forms.MouseButtons.Left Then
      Me.Left += e.X - lx
      Me.Top += e.Y - ly
    Else
      lx = e.X : ly = e.Y
    End If
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
 
Creating a random new point within a picturebox bounds
Creating a random new point within a picturebox bounds
 
-->