Drawing angled lines with user specified angles

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
sbucher
Posts: 5
Joined: Mon Jan 19, 2009 12:47 pm

Drawing angled lines with user specified angles

I'm trying to write a program for my sinclair that requires drawing lines at various angles using user specified degree input.
I'm having trouble finding the correct way to do this.
Can anyone give me some pointers? or perhaps a sample line of code?
thanks...

siggi
Posts: 882
Joined: Thu May 08, 2008 9:30 am
Location: Dauernheim, Germany
Contact:

Re: Drawing angled lines with user specified angles

This is a fast line drawing routine written in C. Since it uses only integer arthmetic and add/subtract/shift operations, it can easily be ported to assembler.
Using BASIC with the ZX81 is also possible, but would be very slow, because everything would be calculated using floating-point arithmetic.
Of course you still need the sin oder cos functions first to calculate the destination coordinates of the line depending on the given angle.

Code: Select all

//---------------------------------------------------------------------------
//draws a line from x1, y1 to x2, y2; line can be drawn in any direction
//set show to 1 to draw pixel, set to 0 to clear pixel
//---------------------------------------------------------------------------
void DrawLine(int x1, int y1, int x2, int y2, unsigned char show)
{
int dx, dy, stepx, stepy, fraction;

dy = y2 - y1;
dx = x2 - x1;

if (dy < 0)
{
dy = -dy;
stepy = -1;
}
else
{
stepy = 1;
}

if (dx < 0)
{
dx = -dx;
stepx = -1;
}
else
{
stepx = 1;
}

dy <<= 1;
dx <<= 1;

LCD_SetPixel(x1, y1, show);

if (dx > dy)
{
fraction = dy - (dx >> 1);
while (x1 != x2)
{
if (fraction >= 0)
{
y1 += stepy;
fraction -= dx;
}

x1 += stepx;
fraction += dy;
LCD_SetPixel(x1, y1, show);
}
}
else
{
fraction = dx - (dy >> 1);
while (y1 != y2)
{
if (fraction >= 0)
{
x1 += stepx;
fraction -= dy;
}
y1 += stepy;
fraction += dx;
LCD_SetPixel(x1, y1, show);
}
}
}
HTH Siggi
My ZX81 web-server: online since 2007
http://zx81-siggi.endoftheinternet.org/index.html

sbucher
Posts: 5
Joined: Mon Jan 19, 2009 12:47 pm

Re: Drawing angled lines with user specified angles

Hi,
I'm ok with drawing straight lines and inputting the line lengths. What I'm having trouble with is writing code to plot angled lines.
Let's say I want to start at point 0,22 and draw a straight line 1.75 inches.
Now I want to start at the same point, draw another 1.75 inch line, but... this line goes off at a 54.5 degree angle from the starting point.
This is where I'm stuck. I'm not sure what the sinclair basic line of command would be for this...

RWAP
Posts: 1314
Joined: Thu May 08, 2008 8:42 am
Location: Stoke-on-Trent, UK
Contact:

Re: Drawing angled lines with user specified angles

Ah for that you will need to remember what the angle is at all times and use SIN and COS (sine and co-sine) calculations to work out the change to the current x,y co-ordinate to find the end of the line.

x=x+(SIN(angle)*length)
y=y+(COS(angle)*length)

sbucher
Posts: 5
Joined: Mon Jan 19, 2009 12:47 pm

Re: Drawing angled lines with user specified angles

I'm having a bit of trouble trying to put the whole thing together. Any chance of you writing a sample line of code in s-basic as a example? I think if I can see an example that'll help.
I'm trying to write a program that does "tangent projection" that is,drawing a series of lines at various angles to define a unfolded pattern.

sbucher
Posts: 5
Joined: Mon Jan 19, 2009 12:47 pm

Re: Drawing angled lines with user specified angles

Hi,
I finally got this figured out, I got a couple books on sinclair basic and some other advise and it's working.
Thanks for your time and suggestions, they were a big help and appreciated...

RWAP
Posts: 1314
Joined: Thu May 08, 2008 8:42 am
Location: Stoke-on-Trent, UK
Contact:

Re: Drawing angled lines with user specified angles

Why not post the relevant code, to help others in a similar situation?

sbucher
Posts: 5
Joined: Mon Jan 19, 2009 12:47 pm

Re: Drawing angled lines with user specified angles

Here's the code. It'll print the x and y coordinates of an angled line endpoint to a line of user specified length and angle.
For the sake of experimenting I assumed 1" would be 12 pixel movements but I know that's probably not right. I'll fiix that later. Right now I was just interested in getting it to work.

Code: Select all

1 PRINT “ANGLE=: “;
2 INPUT A
3 CLS
4 PRINT “LINE LENGTH=: “;
5 INPUT B
6 CLS
7 LET C= B*12
10 LET ANGLE=A*PI/180
20 LET STARTX = 0
30 LET STARTY = 2
40 LET ENDX = STARTX + C* COS (ANGLE)
50 LET ENDY = STARTY + C* SIN (ANGLE)
60 PRINT “X=: “;
70 PRINT “Y=: “;