• Aucun résultat trouvé

In Amiga BASIC the statements that color a single dot on '—[

Dans le document .n INSIDE AMIGA Graphics (Page 97-100)

the screen are PSET and PRESET. The two statements are

identical, except PSET uses the foreground pen as its default » I drawing pen and PRESET uses the background pen. The syn

tax is

PSET [STEP] (x,y) [,pen] LJ

PRESET [STEP] (x,y) [,pen]

There are two ways of indicating the position at which ] I you want the dot drawn. The first is to use absolute horizontal —

82

LJ

Drawing Lines and Shapes

and vertical coordinates. The horizontal coordinates range from 0 at the left edge of the screen to a maximum of 631 or 311 at the right edge of the screen for a full-size window, de-pending on whether your screen is high-resolution or low-resolution. If you have a sizing gadget in the right border of the window, the maximum is cut to 617 or 297. The vertical coordinates range from 0 at the top of the screen to 186 or 386 at the bottom, depending on whether the screen is noninter laced or interlaced. If there is no title bar, the coordinate at the bottom of the screen is 195 (noninterlaced) or 395 (interlaced).

If your window is smaller, of course, you should use values that are less than the width and height of the current output window. You can find these values by using the WINDOW(2) function to return the window's width and the WINDOW(3) function to return its height. The x and y coordinates that you specify are relative to the top left corner of the window, re gardless of where the window is positioned on the screen.

To put a white dot (the default color of the default fore ground pen) midway down the left edge of the standard out put window on the Workbench Screen, you would use PSET (0,98)

To erase that dot (by drawing over it with the background pen), you could use

PRESET (0,98)

Relative Coordinates

The other way to specify the point at which to draw the dot is to indicate that you wish to use relative coordinates by includ-ing the keyword STEP. Relative coordinates specify a position relative to the last dot drawn. If none has been drawn yet, the position is relative to the middle of the output window (in-eluding the borders). For a full-size low-resolution, noninter laced window, for example, this position would be (160,100).

A positive horizontal coordinate indicates that the dot will be positioned to the right of the last one, while a negative coordi nate moves the dot to the left. A positive vertical coordinate means that this dot is drawn lower than the last one, and a negative vertical coordinate means it is drawn closer to the

83

statement would draw the next dot at (90,70):

PSET STEP (-10,20)

If the last dot drawn was at position (150,90), this statement would draw a dot at (110,80):

PSET STEP (-40,-10)

Relative coordinates are extremely useful when you wish to draw the same image in different places, or when you aren't quite sure where the image will be drawn.

Let's say, for example, that you are drawing an image in a window that has a sizing gadget. If the user leaves the win dow alone, the right edge may be at position 600. But if he or she shrinks the window, its right edge may be only at position 400. You can find the right edge with the WINDOW function and set the first point accordingly. By using relative coordi nates for the rest of the drawing statements, all of them will then be positioned properly, regardless of where the right edge of the window is. The other advantage of using relative co ordinates is that they can make it easier to change your pro gram. If you later decide that you want to move an image over a few pixels, it is much easier just to change the starting point than to change the coordinates for every point.

Pen Color

Both the PSET and PRESET statements take an optional pen value. That value, if specified, selects the pen to be used in drawing the dot. If none is specified, the PSET statement uses the color register associated with the foreground pen, and PRESET uses the color register associated with the background pen. The foreground and background pen values default to color registers 1 and 0, respectively. You can change these as-signments at any time, however, by using the COLOR state-ment (see below). Note that when you specify the pen to use, PSET and PRESET can be used interchangeably; the only dif-ference between them is the default pen that each uses.

84

u

Drawing Lines and Shapes

Which Pen?

Sometimes it is useful for a program to be able to tell what pen was used to color a particular location in a window. The operating system provides a routine called ReadPixel which does just that. A call to this function is of the form

Pen = ReadPixel (RastPort, X, Y);

P~j (dO) (al) (dO) (dl)

The value RastPort is the address of the window's RastPort structure. The x and y values stand for the horizontal and ver tical coordinates of the point that you wish to read. If the point lies within the area of the rastport, the value returned will be the number of the pen with which the dot is colored. If the point is out of range of the rastport, a — 1 is returned.

In Amiga BASIC, the POINT function returns the same information. The syntax of this function is

Pen = POINT(x,y)

where x and y specify the horizontal and vertical coordinates of the point to be read. Like ReadPixel, this function returns the pen number used to color the point if it lies within the area of the window. If the point lies outside the current output window boundaries, the function returns a value of —1.

PSET (100,50),3 T>raw at 100,50 with pen 3 Pen& = POINT(100,50) 'Read dot at 100,50 into Pen&

PRINT Pen& 'Should be 3, for pen 3

This program draws a dot at (100,50) with pen 3, then reads the pen value at (100,50) into the variable Pen&. The value of Pen& is printed to confirm that it has read the pen number r—i correctly.

Drawing Lines and Shapes

f—1 Drawing single points is the least of the Amiga's abilities.

Amiga BASIC and the operating system also contain com mands that allow you to draw lines and entire geometric

Dans le document .n INSIDE AMIGA Graphics (Page 97-100)