Friday, December 6, 2013

HP Prime Programming Tutorial #7: TEXTOUT

Greetings everyone. It has been too long since I last posted. I hope everyone had a fantastic Thanksgiving. For those of you who are battling cold and stormy weather, please be safe. For those of you living in the Southern Hemisphere where summer is about to begin... I'm jealous! :) I love summer.

Introduction

OK this section we will start covering some of the graphics features of the HP Prime Programming Language. We touched on graphics a bit when we used STARTAPP and STARTVIEW to call up the Plot screen of certain apps (Function, Parametric, Polar, Advanced Grpahing).

This time we are going to use drawing commands that can be used in any HP Prime app. In a sense we are creating a graphic object (GROB). The HP Prime allows for ten graphic objects, named G0 - G9. For this tutorial series, (unless specified) I am always going to use the default GROB, G0. This makes typing commands much easier.

Cartesian vs Pixel

Each graphic object operates in either one of two coordinate systems: Cartesian and pixel. If you worked with the Hewlett Packard HP 39gii calculator, this might look familiar to you.

The features of the Cartesian system (x,y) are these:

* The end points depend on the Plot variables Xmin, Xmax, Ymin, and Ymax.
* The system is familiar, having x increasing as we move to the right and y increasing as we move up. (no shocker there).
* The trade is that some (very few) drawing commands don't accommodate the Cartesian system. An example is the ARC command, which requires the radius to be in pixels.

Below is a map of the Cartesian system:

The Pixel System (x,y):

* The boundaries are fixed. The pixel (0,0) is the top left hand corner, the pixel (318, 218) is the lower right hand corner.
* The value of x still increases as we go to the right. However, y increases as we go down, opposite of the Cartesian system. On the other hand, x and y are always non-negative.

The Drawing Commands

The HP Prime has two sets of drawing commands: one for the Cartesian system and one for the Pixel system. All commands for the Pixel system will have a "_P" suffix attached. For example:

LINE draws a line using Cartesian coordinates, while LINE_P draws a line using Pixel coordinates.

General Access: (in the Programming Editor)

Drawing Commands for the Pixel system: Cmds, 2. Drawing, 6. Pixels
Drawing Commands for the Cartesian system: Cmds, 2. Drawing, 7. Cartésian

Clearing the GROB screen

To clear the GROB screen, we will simply type RECT(). The wipes the screen, leaving it white. It is necessary to do this at least at the beginning of each program containing drawing commands. In a sense, RECT() is similar to PRINT().

Hint: To paint an entire screen a specific color, use RECT(color).


Showing the Graphics Screen

It is not enough to type the drawing commands. We need a command to tell the HP Prime to show the graphics. Two ways to do it are:

FREEZE: This does exactly what it says, freezes the screen. To exit, tap the screen or press ESC. Pressing Enter will re-execute the program.

Access: Cmds, 2. Drawing, 3. FREEZE

WAIT(0): This freezes the screen for an indefinite amount of time. However, pressing any button will cause the program to continue. Of course, if the last END is followed by WAIT(0), the program terminates.

Of course, you can use WAIT(n) to make the calculator wait n seconds before executing the next step.

TEXTOUT and TEXTOUT_P

TEXTOUT and TEXTOUT_P inserts text on a graphics object using Cartesian and Pixel coordinates, respectively. They are also at the bottom of the Cartesian and Pixel Drawing sub menus, respectively. (Use either the x,t,θ,n button or the up button followed by Enter).

Full Syntax (starred commands are optional):

Cartesian:
TEXTOUT(text, GROB*, x, y, font size*, text color*, width*, background color*)

Pixel:
TEXTOUT_P(text, GROB*, x, y, font size*, text color*, width*, background color*)

text: The text to be written. It can be a string, results, calculations, or any combination.

GROB*: Graphic Object G0 through G9 to be used. If left out, G0 is used.

x: x coordinate

y: y coordinate

font size*: The text font's size code. Must be used if you want text to be a color other than black. Optional. Default is the current size set by Home Settings.
0: Current font size as set by Home Settings screen.
1: Size 10 font
2: Size 12 font
3: Size 14 font
4: Size 16 font
5: Size 18 font
6: Size 20 font
7: Size 22 font

text color*: The color of the text. Use of the RGB command is advised. Optional. Default color is black.

width*: Length of the background box of the text. Optional. I usually don't use this argument.

background color*: Color of the background box. Optional. I usually don't use this argument.

Simplified Syntaxes:

Black text at default font size:
TEXTOUT(text, x, y)
TEXTOUT_P(text, x, y)

Colored text at a set font size:
TEXTOUT(text, x, y, size code, color)
TEXTOUT_P(text, x, y, size code, color)

With all this, we finally get to some programming. Since it is December, and snowing in a lot of the northern side of Earth, let's use TEXTOUT_P to draw snowflakes. I am going to use symbolize the snowflake by the asterisk, the symbol of multiplication in programming. [ × ] types *.


SNOWFLAKE

SNOWFLAKE takes one argument, which is the number of snowflakes to be drawn.

Note: Take note the order of the commands. The order regarding where to draw and generate random numbers is important to get the results you want.

Program:

EXPORT SNOWFLAKE(N)
BEGIN
LOCAL X,Y,Z,I,L0;
L0:={RGB(0,0,255),RGB(178,255,255),
RGB(30,144,255),RGB(0,255,255)};
\\ blue, light blue, dodger blue, cyan

RECT();

FOR I FROM 1 TO N DO
X:=RANDINT(0,304); \\ save some room since text takes pixels
Y:=RANDINT(0,208);
Z:=RANDINT(1,4);
Z:=L0(Z);
TEXTOUT_P("*",X,Y,2,Z);
END;

FREEZE;
END;



Next time - we will work with ARC and LINE. Until then, Happy Day/Evening everyone!


Eddie


This blog is property of Edward Shore. 2013

And in 25 days, it will be 2014!

TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...