Re: How to store relative position
- From: "Mike Williams" <mikea@xxxxxxxxxxxxxxxxx>
- Date: Sat, 12 Jul 2008 07:30:18 +0100
"Carl Stevens" <rudebox2000@xxxxxxxxxxxxxx> wrote in message news:eg0nnk34IHA.4492@xxxxxxxxxxxxxxxxxxxxxxx
I have a better example:
When I'm using a graphic editor software, and I have zoomed my
drawing area out 50%, and I paint a dot, then zoom the drawing
area in again to 100%... How does the graphic editor software
store where I made the dot when the drawing area was zoomed
out to 50%? Do they use percent or how do they do it?
It treats the drawing area as being exactly the same logical height and logical width, regardless of its current physical width. For example, if your drawing area is meant to represent a full US Letter Size page (8.5 inches x 11 inches) then it treats the displayed drawing area as being 8.5 units wide and 11 units high at all times, regardless of the current physically displayed pixel width and height. And, of course, it uses floating point values to record the size and position of every drawn element on the page. That is exactly how the VB will treat your PictureBox drawing area if you ignore its current physically displayed size and set its ScaleWidth to 8.5 and its ScaleHeight to 11, as has already been suggsted to you by a number of people who have posted reponses to your question. You can do this yourself in whatever way you wish, but the PictureBox and its various Scale properties makes it easy for you.
It is of course convenient to always display the drawing on screen at the same aspect ratio as an 8.5 x 11 inch page so that the user can always see the approximate real "shape" of the page. That is not actually necessary, but it is a wise thing to do. You then just record the position and size of each element using 8.5 x 11 coordinates. If, for example, you wish to draw a single dot in the centre of that page then you would record the dot's position as (4.25, 5.5) and that is the value you would store in your data that represents the "drawn page", regardless of the current displayed pixel size of the drawing (its current visible scale). Your code then treats that "drawn dot" as a single object (if it has been drawn as a single object) or as a coordinate on a line (if it has been drawn as a freehand line). The object (the dot or the line) will of course have a "size" (or a thickness) as well as a positional coordinate, so you need to store that information as well. It's size of course will depend on the size of the "drawing tool" that the user chose before he drew the dot and, of course, it will be a "logical size", in the same way that its coordinates are "logical coordinates", so its size will also be stored as a floating point number, representing its "thickness" in the same scale units. You don't really ever need to worry about the physical "on screen pixel" coordinate of the dot. The coordinate (4.25 x 5.5) is all you need to store as far as its position is concerned. The VB PictureBox Scale stuff will look after all this for you.
How does the graphic editor software store where
I made the dot when the drawing area was zoomed
out to 50%? Do they use percent or how do they do it?
If you are specifying the coordinates of the dot yourself in code, or in accordance with input data typed into some text boxes by the user, then you can position it anywhere you want on the "page", with as much resolution as you wish (as in the example of 4.25 x 5.5 above), but if the user is specifying the coordinates of the dot by clicking the mouse on your "displayed representation of the page" then the user can only position the dot at the current position of the mouse, as it relates to the page. Since the mouse has a restricted resolution, and since your code generally knows where it is only to the nearest screen pixel, then that places a limit on how accurately the user can specify the position of his "dot". All drawing applications have this limitation as far as user mouse input is concerned, and in order to give the user a higher accuracy for his positioning then you need to show him a larger physical "representation" of your 8.5 x 11 inch page, perhaps in a scrollable container. All this is very easy to do using VB Picture Boxes.
For example, if your 8.5 x 11 inch page is currently being displayed on screen as a 371 x 480 pixel image (the nearest you can get to 370.9091 x 480 pixels) then you would set the Scale units of the Form (the PictureBox's container) to pixels and set the size of the borderless PictureBox to 371 x 480 pixels. You would then set the PictureBox ScaleWidth to 8.5 and its ScaleHeight to 11. If the user wishes to position a dot anywhere on that representation of your 8.5 x 11 inch page (using only the mouse as the input device) then at this specific scale he is restricted to a resolution of 11/480 logical inches, although the resolution is very slightly different in the x and y planes because the displayed pixel size is almost never exactly in the ratio 8.5 : 11. For example, if the user wishes to position the dot at exactly (3, 5) inches on the page using only the mouse as the input device then he cannot actually do so, because the mouse can only be at a whole pixel position. As far as your scaled drawing is concerned the nearest he can get is about (131, 218) pixels, which as far as the scaled VB PictureBox is concerned is the equivalent of (3.001, 4.996) logical inches. When the user clicks the mouse at that position then the values you store for the coordinates are 3.001 and 4.996, representing inches, since you cannot automatically assume that the user meant anything other than those values. He might indeed have been trying to position the mouse at exactly 4.996 inches, or perhaps at 4.99 or 4.995 inches. You can only go by the whole pixel position of his input when he is using the mouse, which in this case is 3.001 inches for the x coordinate and 4.996 inches for the y coordinate. All drawing applications have to cope with these things.
In fact the values you need to store will be given to you by the PictureBox MouseMove event in this specific PictureBox. This same reasoning applies regardless of the scale you are currently showing the PictureBox. All drawing applications have to put up with this same limitation as far as "mouse only" input is concerned. If you want to give the user a higher resolution, thereby allowing him to manually move the mouse closer to his desired (3, 5) inch coordinate, then you must display the PictureBox at a larger physical pixel size, perhaps in a scrollable container. The same reasoning applies regardless of how you are storing the image data. Even if you are storing it as a metafile, as many drawing applications do and as indeed you can also do in your Visual Basic program, you are still limited, as far as resolution is concerned regarding user mouse input, by the current physical size of the drawing as it is displayed on screen (the current "scale").
Exactly the same reason applies if your "page" is actually meant to represent a 3200 x 2400 bitmap, rather than an 8.5 x 11 inch page. In that case if you want to display the image at a current physical size of 640 x 480 pixels then you set the physical pixel size of the borderless PictureBox to 640 x 480 and set its ScaleWidth to 3200 and its ScaleHeight to 2400. The VB PictureBox will look after the rest for you.
Here (below) is a very simple example of a PictureBox set up to represent and 8.5 x 11 inch page, so that you can see for yourself how VB sorts out the coordinates for you. Paste the code into a VB Form containing a PictureBox.
Mike
Option Explicit
Private Sub Form_Load()
Me.ScaleMode = vbPixels
With Picture1
.BorderStyle = vbBSNone
.BackColor = vbCyan
.Move 20, 20, 371, 480
.ScaleWidth = 8.5
.ScaleHeight = 11
End With
End Sub
Private Sub Picture1_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Picture1
Caption = Format(X, "0.000") & " x " _
& Format(Y, "0.000") & " : " _
& .ScaleX(X, .ScaleMode, vbPixels) _
& " x " _
& .ScaleY(Y, .ScaleMode, vbPixels)
End With
End Sub
.
- References:
- How to store relative position
- From: Carl Stevens
- Re: How to store relative position
- From: Bob Butler
- Re: How to store relative position
- From: Carl Stevens
- How to store relative position
- Prev by Date: Re: How To handle On Error when processing a For-Next loop...
- Next by Date: Re: Textbox.Height.
- Previous by thread: Re: How to store relative position
- Next by thread: Re: How to store relative position
- Index(es):
Relevant Pages
|