Placing Objects Exactly on the Screen
- Michael Horvath
Sometimes you might want to place objects at particular places on the screen, or create objects having particular pixel dimensions. Unfortunately, you cannot configure GeoGebra to use screen dimensions as the default set of units (for instance, you can do this in Geometer's Sketchpad), and the other option - extracting and editing the internal XML file - is difficult and subject to being compromised next time you open the file in GeoGebra. However, GeoGebra does have in internal parameter storing the size of the drawing pane in pixels, and you can access this parameter using the special command,
cornercommand takes a number of different parameters: if you pass the numbers (integers) 1, 2, 3 or 4 to the command, it generates points at the bottom-left, bottom-right, top-right and top-left corners of the drawing pane, respectively. More useful for our purposes, however, is what happens when you pass the number 5 to the command. When you pass the number 5 to the
cornercommand it creates a point whose coordinates are equal to the size of the drawing pane in pixels. For instance, if the drawing pane is 1000 pixels wide and 700 pixels tall, the new point will have the coordinates (1000, 700). What this allows us to do is calculate the dimensions of the drawing pane in relation to the drawing's coordinate units. I.e. if you know the locations of the four corners of the drawing pane with respect to the origin, and you know the dimensions of the drawing pane in pixels, you can calculate the distance between the four corners in the drawing's base coordinate units. From that it's fairly trivial to calculate the size of the coordinate units in pixels and place an object exactly where you want on the screen.
The first thing we'll need to do is get the coordinates of the four corners using the
cornercommand. In the input bar, type:
A = corner B = corner C = corner D = cornerNext we want to create the point from which we can determine the screen dimensions of the drawing pane. Type:
screenDim = cornerNext we want to determine the size of the drawing pane in the drawing's native coordinate units. Type:
distX = Distance[A, B] distY = Distance[A, D]Now we can calculate the scale of the coordinate units in pixels. Type:
unitX = x(screenDim) / distX unitY = y(screenDim) / distYI.e. the distance between each tick of the coordinate axes is equal to
unitXin the x direction and
unitYin the y direction. (Alternately, you can skip creating one of
unitYand just use a single value for the scale of both of the coordinate axes.) We now have everything we need to begin placing our point. First of all, let's decide where on the screen we want to place it. Let's place it at 500 pixels from the top of the drawing pane, and 500 pixels from the left of the drawing pad. Type:
testXpix = 500 testYpix = 500Now let's convert these pixel dimensions into coordinate points. Type:
testXcoo = x(A) + testXpix / unitX testYcoo = y(D) - testYpix / unitYWe subtract the y coordinate from point D because the coordinate axes increase toward the top and right, whereas screen coordinates are traditionally given from the top-left corner to the bottom and right. Lastly, we place our actual point:
G = (testXcoo, testYcoo)As we can see, our point extends downward from the top and left of the drawing pane. It also forms a square with respect to the drawing pane's edges, so we know we've done our calculations correctly. Most importantly, the point remains fixed on the screen as we scroll our drawing from the left to the right and also when we zoom in and out. We can now place a point or arbitrary object on the screen and be confident that it will remain there and not move as we make further changes to our document.
That concludes our tutorial. If you want to continue to experiment you can try placing other types of objects, or objects that take multiple points of input, such as polygons. Further, if you want to do something different like determine the exact screen coordinates of an existing object that is not already positioned exactly, the methods are not that different. If you continue trying and make additional efforts your efforts are bound to be successful.