Drawing Lines on the Screen

SINE next calls the subroutine drawlines, which draws a rectangle around the outer edges of the screen and three horizontal lines that divide the screen into quarters. (See Sine Program Output.)

 !  DRAWLINES - This subroutine draws a box and
 !  several lines.

 SUBROUTINE drawlines( )
    USE DFLIB

    EXTERNAL        newx, newy
    INTEGER(2)      status, newx, newy, maxx, maxy
    TYPE (xycoord)  xy
    COMMON          maxx, maxy
 ! 
 !  Draw the box.

    status = RECTANGLE( $GBORDER, INT2(0), INT2(0), maxx, maxy )
    CALL SETVIEWORG( INT2(0), newy( INT2( 500 ) ), xy )    ! This sets
 !           the new origin to 0 for x and 500 for y. See comment after subroutine.

 !  Draw the lines.

    CALL MOVETO( INT2(0), INT2(0), xy )
    status = LINETO( newx( INT2( 1000 )), INT2(0))
    CALL SETLINESTYLE( INT2( #AA3C ))
    CALL MOVETO( INT2(0), newy( INT2( -250 )), xy )
    status = LINETO(newx( INT2( 1000 )),newy( INT2( -250 )))
    CALL SETLINESTYLE( INT2( #8888 ))
    CALL MOVETO(INT2(0), newy( INT2( 250 )), xy )
    status = LINETO( newx( INT2( 1000 )),newy( INT2( 250 ) ) )
 END SUBROUTINE

The first argument to RECTANGLE is the fill flag, which can be either $GBORDER or $GFILLINTERIOR. Choose $GBORDER if you want a rectangle of four lines (a border only, in the current line style), or $GFILLINTERIOR if you want a solid rectangle (filled in with the current color and fill pattern). Choosing the color and fill pattern is discussed in Adding Color and Adding Shapes.

The second and third RECTANGLE arguments are the x- and y-coordinates of the upper-left corner of the rectangle. The fourth and fifth arguments are the coordinates for the lower-right corner. Because the coordinates for the two corners are ( 0, 0 ) and ( maxx, maxy ), the call to RECTANGLE frames the entire screen.

The program calls SETVIEWORG to change the location of the viewport origin. By resetting the origin to (0, 500) in a 1000x1000 viewport, you effectively make the viewport run from (0, -500) at the top left of the screen to (1000, 500) at the bottom right of the screen:

  CALL SETVIEWORG( INT2(0), newy( INT2( 500 ) ), xy )

Changing the coordinates illustrates the ability to alter the viewport coordinates to whatever dimensions you prefer. (Viewports and the SETVIEWORG routine are explained in more detail in Understanding Coordinate Systems.)

The call to SETLINESTYLE changes the line style from a solid line to a dashed line. A series of 16 bits tells the routine which pattern to follow. A "1" indicates a solid pixel and "0" an empty pixel. Therefore, 1111 1111 1111 1111 represents a solid line. A dashed line might look like 1111 1111 0000 0000 (long dashes) or 1111 0000 1111 0000 (short dashes). You can choose any combination of ones and zeros. Any INTEGER(2) number in any base is an acceptable input, but binary and hexadecimal numbers are easier to envision as line-style patterns.

In the example, the hexadecimal constant #AA3C equals the binary value 1010 1010 0011 1100. You can use the decimal value 43580 just as effectively.

When drawing lines, first set an appropriate line style. Then, move to where you want the line to begin and call LINETO, passing to it the point where you want the line to end. The drawlines subroutine uses the following code:

  CALL SETLINESTYLE(INT2( #AA3C ) )
  CALL MOVETO( INT2(0), newy( INT2( -250 ) ), xy )
  dummy = LINETO( newx( INT2( 1000 )), newy( INT2( -250 )))

MOVETO positions an imaginary pixel cursor at a point on the screen (nothing appears on the screen), and LINETO draws a line. When the program called SETVIEWORG, it changed the viewport origin, and the initial y-axis range of 0 to 1000 now corresponds to a range of –500 to +500. Therefore, the negative value –250 is used as the y-coordinate of LINETO to draw a horizontal line across the center of the top half of the screen, and the value of 250 is used as the y-coordinate to draw a horizontal line across the center of the bottom half of the screen.