2017-10-04 43 views
0

我有一個程序,我可以繪製兩條線,當我選擇這些線的起點和終點時,它會計算它們相交的點。我想從交點開始畫一條線,恰好在這兩條線的中間。java兩條線的交點的繪製線

example

即時計算兩條線之間的角是這樣的:

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; 
} 

,然後生成這樣的新行:

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); 

其中pI是交點。然而,這條線總是面臨錯誤的角度,我怎麼能重寫這段代碼,使得這條線完全被繪製在另外兩條線之間,比如上面的示例圖片?

編輯:

輸出:example2

+0

能否請你告訴我們你的方法的視覺輸出? – Maia

+0

@Maia增加了輸出 – FrankK

+0

哪一點是兩條線上的第一點?你想要哪條線去?也許用另一種顏色添加它。 – matt

回答

0

我不知道你用的角度做什麼,但我想你想的第二點是:

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

角度你正在計算的是一條線通過每條線的第一個點所畫的角度。這似乎不相關。

+0

多數民衆贊成它,謝謝 – FrankK

0

要找到由兩條線組成的角度的平分線,您需要計算這些線的單位方向矢量。

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 

和平分線的第二個點是

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