header

Lab 4 - World Coordinates

For math and science types, drawing using pixel coordinates is unnatural and limiting. It is unnatural because in the device coordinate system, the y-axis goes down rather than up. It is limiting in that the upper-left corner of the screen is always the origin and the coordinates of every point are positive integers. There is no provision for negative coordinates nor for non-integer coordinates.

It is usually much easier to frame a graphing problem in terms of the Cartesian coordinate system than in terms of the pixel coordinates of the graphics device. Suppose I want to draw a square whose sides measure 3.0 centered at the point (-2.4, 3.1) on a Cartesian coordinate system that ranges from -5 to 5 in both directions. How do I even talk about such a problem in a coordinate system limited to non-negative integer coordinates?

The solution is create a mapping from a window on the normal Cartesian coordinate system to the pixel-based coordinate system of the graphics screen. Coordinates on this window are referred to as world coordinates. Using such a mapping, a graphics application can accept user input based on the normal Cartesian coordinate system, convert the coordinates of each point to the corresponding pixel coordinates, and draw the image on the graphics screen.

In the illustration below, the world coordinates (in red) and the pixel coordinates (in black) are given for several different points. The graphics window is 201 pixels high and 201 pixels wide and represents a window on the Cartesian plane that runs from -5 to 5 in both directions.

World vs. Screen Coordinates

Mapping Function for the X-Coordinate

The mapping from world coordinates to screen coordinates is a linear function whose equation is determined by two points lying on the line. First, let's consider the mapping of the x-coordinate in the world system to the corresponding screen coordinate:

screenX = worldX * a + b

Two points that lie on this line are (-5,0) and (5,200). When worldX is -5 then the corresponding screenX is 0 and when the worldX is 5 then the corresponding screenX is 200. Using these two points, we obtain the following pair of simultaneous linear equations:

   0 = -5a + b
200 =  5a + b

Solving this system of equations leads to the solution a = 20 and b = 100 and the mapping function is given by:

screenX = 20*worldX + 100

Mapping Function for the Y-Coordinate

The mapping function for y-coordinates has the form:

screenY = worldY * c + d

Two points on this line are (-5,200) and (5,0). When worldY is -5 then the corresponding screenY is 200 and when the worldY is 5 then the corresponding screenY is 0. Using these points, we obtain the following pair of simultaneous linear equations:

200 = -5c + d
  0 =  5c + d

Solving this system of equations leads to the solution c = -20 and d = 100 and the mapping function is given by:

screenY = -20*screenY + 100

Putting the Pieces Together

The graphics functions in winBGIm require screen coordinates. To use these functions, each point in the world coordinate system (double values) must be mapped to the corresponding point in the screen coordinate system (integer values):

screenX = int( 20.0 * worldX + 100.0 + 0.5) = int( 20.0 * worldX + 100.5)
screenY = int(-20.0 * worldY + 100.0 + 0.5) = int(-20.0 * worldY + 100.5)

In both functions, adding 0.5 before converting to an integer value will result in rounding the double result to the nearest integer rather than simply truncating to the integer part.

The Assignment

In this assignment, you are to implement a class named "worldType" that handles the mapping from world to screen coordinates. The file worldType.h contains a skeleton version of this class. The file "Lab04.ccp" contains an application that tests your worldType class implementation.

Notes

1. All the reset function does is recalculate the coefficients of the mapping functions. It should not initialize another window because the window for this world has already been initialized by its constructor.

2. Notice that the moveTo function does nothing more than invoke the graphics package moveto function with the pixel coordinates that correspond to the specified world coordinates. The other graphics functions in the worldType class work the same way.

3. Notice that the parameter to each mapping function is passed by address in order to make the function faster. As we did last semester, we declared the parameter as const so the function can not change its value (because it is an in parameter).

File Submission

Email me your Lab04 project file (PRJ) and your worldType header file (worldType.h).