1
votes

I have a program where i can draw two lines, when i have selected the start and end point for those lines, it calculates the point where they will intersect. I want to draw a line, starting from the intersection point, exactly in the middle of those two lines.

example

im calculating the angle between the two lines like this:

double angle(Line pL1, Line pL2){
    double angle = Math.toDegrees(Math.atan2(pL2.p1.y - pL1.p1.y, pL2.p1.x - pL1.p1.x));
    if(angle < 0){
        angle += 360;
    }
    return angle;
}

and then generate the new line like this:

    double newAngle = Math.toRadians(drawAngle);
    System.out.println(newAngle);
    double x = pI.x + 80 * Math.sin(newAngle);
    double y = pI.y + 80 * Math.cos(newAngle);
    Point2D.Double endPoint = new Point2D.Double(x,y);
    Line l3 = new Line(pI,endPoint);

where pI is the intersection point. however, the line always ends up facing to the wrong angle, how can i rewrite this code so that the line gets drawn exactly inbetween the two other lines, like the example picture above?

EDIT:

output: example2

2
Can you please show us the visual output of your method? - Maia
@Maia added the output - FrankK
Which point is point one on the two lines? Where do you want the line to go? Maybe add it in another color. - matt

2 Answers

0
votes

I don't know what you're doing with the angle, but I think the second point you want is:

double y = 0.5*(pL2.p1.y + pL1.p1.y);
double x = 0.5*(pL2.p1.x + pL1.p1.x);

The angle you are calculating is the angle a line draw through the first point of each line makes. Which appears to not be relevant.

0
votes

To find bisector of angle formed by two lines, you need to calculate unit direction vectors for these lines.

len1 = Sqrt((pL1.p1.y - pL1.p2.y)^2 + (pL1.p1.x - pL1.p2.x)^2)
dx1 = (pL1.p2.x - pL1.p1.x) / len1
dy1 = (pL1.p2.y - pL1.p1.y) / len1
similar for the second line

bx = (dx1 + dx2) / 2
by = (dy1 + dy2) / 2

and the second point for bisector is

point.x = pI.x + 100.0 * bx 
point.y = pI.y + 100.0 * by