# Draw a line without using line()

Hey guys,

I’m new at processing and I would like to do a simple task: draw a line without using the line() function.

``````void setup() {
size(1300, 800);
}
float x1=100;
float x2=200;
float y1=1;
float y2=779;
float m=(y2-y1)/(x2-x1);

void draw()
{
drawAline(x1,y1,x2,y2);
}
void drawAline(float xa,float ya, float xb, float yb){
if(xa<xb){
while(xa<xb){
xa++;
ya=m+ya;
point(xa,ya);
}
}
if(xa>xb){
while(xa>xb){
xa--;
ya=ya-m;
point(xa,ya);
}
}
if(xa==xb){
if(ya>yb)
{
while(ya>yb){
yb++;
point(xb,yb);
}
}
if(yb>ya){
while(yb>ya){
ya++;
point(xa,ya);
}
}
if(ya==yb){
point(xa,ya);
}
}
}
``````

In some ways it works well because the line is continious, but sometimes the line is dashed. What else should I add to make it work?

I hope you can help me on this! Thanks!

1 Like

Hello!

The line is dotted if m is larger than 1.
ya increases whith m and the next dot is further away than 1 ==> dotted.
You need to increase xa i smaller steps:

``````void drawAline(float xa, float ya, float xb, float yb) {
if (xa<xb) {
while (xa<xb) {
xa+=1/m;
ya++;
point(xa, ya);
}
``````

Hope this points in the right direction.

2 Likes

If it is dotted, it may be because the distance between two successive points is too great.
You can try working with the vectors.
Starting from the first point and successively adding the direction vector (calculate with your two points) with a magnitude set to 1, you will arrive at the last point of your segment.

The use of vectors simplifies the work a lot (by reducing the number of lines of code)

2 Likes

Thanks! It works now!