**HP Prime: Drawing 3D Lines and Boxes**

**Introduction**

I was sent an email from Carissa. One of the questions was to explain the 3D features of LINE and TRIANGLE commands. Confession: I have never used to the 3D features of LINE and TRIANGLE before, time to learn something new. Here is what I learned.

**The Command LINE**

In 3D drawing, you can connect as many points as you want. In the most simple form, LINE takes three arguments:

* Point Definition

* Line Definition

* Rotation Matrix

**Point Definition**

You define points of three dimensions (x, y, z) and if you want, a color. The color attached to the point dominates any portion of the line that is nearest to the point. The point definition is a nested list.

Syntax: { {x, y, z, [color]}, {x, y, z, [color]}, {x, y, z, [color]}, ... }

Pay attention to the order you set your points. Each point will be tied to an index. For example, the first point in this list will have index 1, the second point in the list will have index 2, and so on. Knowing the index number will be needed for the Line Definition.

**Line Definition**

This is where you assign which lines connect to which points.

Syntax: { {index start, index end, [color], [alpha]}, {index start, index end, [color], [alpha]}, {index start, index end, [color], [alpha]}, ... }

Like the Point Definition, the Line Definition is a nested list. You can make as many points as you want.

Color and alpha in the List Definition overrides any color defined in the Point Definition list.

**Rotation Matrix**

This tells you the command how you want to rotate the matrix with the respect to the x, y, and z axis, respectively. The acceptable size for the matrix is 2 x 2 (for x and y only), 3 x 3 (x, y, and z axis) and 3 x 4 (I'm not sure what the fourth column is for). For this blog entry and in practice, I use the 3 x 3 rotation matrix.

In general:

Rx = [ [1, 0, 0],[0 cos a, -sin a],[0, sin a, cos a] ], rotation about the x axis at angle a

Ry = [ [cos b, 0, -sin b], [0, 1, 0], [sin b, 0, cos b ] ], rotation about the y axis at angle b

Rz = [ [cos c, -sin c, 0], [sin c, cos c, 0], [0, 0, 1] ], rotation about the z axis at angle c

Full Rotation matrix: r = Rx * Ry * Rz

You can create a program to calculate rotation matrix or copy the syntax to use in your drawing program, as shown here:

**HP Program ROTMATRIX**

EXPORT ROTMATRIX(a,b,c)

BEGIN

// rotate x axis

// rotate y axis

// rotate z axis

LOCAL x,y,z,r;

x:=[[1,0,0],[0,COS(a),−SIN(a)],

[0,SIN(a),COS(a)]];

y:=[[COS(b),0,−SIN(b)],[0,1,0],

[SIN(b),0,COS(b)]];

z:=[[COS(c),−SIN(c),0],

[SIN(c),COS(c),0],[0,0,1]];

r:=x*y*z;

RETURN r;

END;

**Drawing the Boxes**

**HP Prime Program: DRAWBOX**

The program DRAWBOX draws a simple box.

EXPORT DRAWBOX()

BEGIN

// 3D line demo

// draw still box demo

// 2019-04-23 EWS

// black background

RECT(0);

// colors - do this first

LOCAL c1:=#FFFF00h; // yellow

LOCAL c2:=#39FF14h; // neon green

LOCAL c3:=#FFFFFFh; // white

// points of the cube

LOCAL p:={{−3,0,0,c1},{0,−2,2,c1},

{3,0,0,c1},{0,2,−2,c2},

{−3,6,0,c1},{0,4,2,c1},

{3,6,0,c1},{0,8,−2,c2}};

// line definitions

// bottom

LOCAL d1:={{1,2},{2,3},

{3,4},{4,1}};

// top

LOCAL d2:={{5,6},{6,7},

{7,8},{8,5}};

// sides, override with white

LOCAL d3:={{1,5,c3},{2,6,c3},

{4,8,c3},{3,7,c3}};

// rotation matrix

LOCAL r:=[[1,0,0],[0,1,0],

[0,0,1]];

LINE(p,d1,r);

LINE(p,d2,r);

LINE(p,d3,r);

WAIT(0);

END;

**HP Prime Program: DRAWBOX2**

DRAWBOX2 takes three arguments, rotation of the x axis, rotation of the y axis, and rotation of the z axis. The arguments are entered in degrees, as the calculator is set to Degrees mode in the program.

EXPORT DRAWBOX2(a,b,c)

BEGIN

// 3D line demo

// draw 3D box

// 2019-04-23 EWS

// rotate the cube

// set degrees mode

HAngle:=1;

// rotation calculation

// rotate x axis

// rotate y axis

// rotate z axis

LOCAL x,y,z,r;

x:=[[1,0,0],[0,COS(a),−SIN(a)],

[0,SIN(a),COS(a)]];

y:=[[COS(b),0,−SIN(b)],[0,1,0],

[SIN(b),0,COS(b)]];

z:=[[COS(c),−SIN(c),0],

[SIN(c),COS(c),0],[0,0,1]];

r:=x*y*z;

// black background

RECT(0);

// colors - do this first

LOCAL c1:=#FFFF00h; // yellow

LOCAL c2:=#39FF14h; // neon green

LOCAL c3:=#FFFFFFh; // white

// points of the cube

LOCAL p:={{−3,0,0,c1},{0,−2,2,c1},

{3,0,0,c1},{0,2,−2,c2},

{−3,6,0,c1},{0,4,2,c1},

{3,6,0,c1},{0,8,−2,c2}};

// line definitions

// bottom

LOCAL d1:={{1,2},{2,3},

{3,4},{4,1}};

// top

LOCAL d2:={{5,6},{6,7},

{7,8},{8,5}};

// sides, override with white

LOCAL d3:={{1,5,c3},{2,6,c3},

{4,8,c3},{3,7,c3}};

// rotation matrix is already

// defined

LINE(p,d1,r);

LINE(p,d2,r);

LINE(p,d3,r);

WAIT(0);

END;

**3D Triangles**

The format for TRIANGLE is similar except the definition list has three points to make up the triangle instead of two. Format: {x, y, z, [ c ]}

Code:

EXPORT TEST6241()

BEGIN

// test 3D triangle

RECT_P();

//TRIANGLE({0,0,#0h,0},

//{2,2,#0000FFh,39},

//{1,5,#FF0000h,−2});

LOCAL c:=#400080h;

LOCAL p:={{0,0,0},{−10,−8,2},

{−6,4,3},{0,0,0}};

LOCAL d:={{1,2,3,c},{1,2,4,c},

{1,3,4,c},{2,3,4,c}};

local rotmat= [[1, .5, 0], [.5, 1, .5], [0, .5, 1]]; // Initial rotation matrix. No rotation but translate to middle of screen

TRIANGLE(p,d,rotmat);

WAIT(0);

END;

Hope this helps and all of our drawing capabilities on the HP Prime are expanded. Carissa, thank you for your email, much appreciated and I learned a great new skill. All the best!

Eddie

All original content copyright, © 2011-2019. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

## No comments:

## Post a Comment