drawing the line using the equation

1.8k views Asked by At

The goal of my program is to draw the lines perpendicular to the white line, passing through yellow points. My only idea was to find the equation, but the first thing I did was to draw the white line using the equation, but it doesn't work and I am wondering why.

enter image description here

I am trying to draw a line between two points. I have a point (beginX, beginY) and (endX, endY)

Here is how the equation looks:

equation

I am using the Marvin Image Processing Library and there is a function

    imageIn.drawLine(int x1, int y1,int x2,int y2,Color c);

which draws a line between two points (x1,y1) and (x2,y2)

I used this function to draw the line between my points, and it works well. But now I want to draw the same line but using my equation. (I need it, because, the next step of my program is to draw the perpendicular lines passing through the yellow points, and I have no idea how to do it in a different way).

Here is my code:

    int A = (endY-beginY)/(endX-beginX);

    int x1 = 120;
    int x2 = 60;

    int y1 = (int)(A*x1+beginY-A*beginX); // equation
    int y2 = (int)(A*x2+beginY-A*beginX); 


    imageIn.drawLine(x1, y1, x2, y2, Color.green);
    imageIn.drawLine(beginX, beginY, endX, endY, Color.white);     

End the effect:

enter image description here

I have no idea, why my line is not the same as the white one. I know that it starts and ends in different points but why it is even no pararrel to the white one?

Does anyone know what is going on? And if not, maybe can anyone tell me the way to draw the perpendicular lines passing through yellow points not using mathematical equation ?

3

There are 3 answers

0
user2921187 On

It seems you may have a problem with your y1 and y2 coordinates. From prior experience and theory-wise, if you cast them into int, your y-coordinates won't be the same as the ones that mark the white line. Try casting earlier, say when you define variable A.

1
user3131037 On

Oh, I found a good, simple solution to draw the perpendicular.

I find two points, laying on the white line, which are located in the same distance from the yellow point. Now when I color the pixels which are at the same distance from each of this two points I get the perpendicular line.

Code:

     for ( int i = 0 ; i < 5; i++){        
     for (int x = 0; x < imageIn.getWidth(); x++) {
        for (int y = 0; y < imageIn.getHeight(); y++) {
            Point xy = new Point(x,y);
            Point temp = new Point(a[i],b[i]); // a[i] b[i] - coordinates of the first point
            Point temp1 = new Point(a[i+2],b[i+2]); // a[i+2] b[i+2] - coordinates of second point
            if(CountDistance(imageIn, xy, temp)==CountDistance(imageIn, xy, temp1))
            {
                imageIn.setIntColor(x, y, 0,0,255);
            }

        }
        }
    }

Effect:

enter image description here

0
mathmistakes On

The line perpendicular to y = mx + b has a slope of m' = -1/m. Now you have a slope m' and the point P = (x, y) (pick the point one line would intersects, or just pick b') and thus you can find y = m'(x) + b'. This is really a simple problem it doesn't need $O(n^3)$ solution.