2016-11-29 112 views

回答

0

你行的公式是

y=ax+b 
a=(y2-y1)/(x2-x1) 
b=(x2*y1-y2*x1)/(x2-x1) 

中點:

x3=(x1+x2)/2.;y3=(y1+y2)/2. 

式垂直線:

y-y3=-1./a*(x-x3) 
y=-1./a*x+x3/a+y3 
a2=-1./a 
b2=x3/a+y3 

gnuplot的腳本:

x1=1.;y1=3.;x2=10.;y2=15. 
a=(y2-y1)/(x2-x1) 
b=(x2*y1-y2*x1)/(x2-x1) 
x3=(x2+x1)/2.;y3=(y2+y1)/2. 
a2=-1./a 
b2=x3/a+y3 
set arrow 1 from x1,y1 to x2,y2 nohead 
plot [0:15][0:22] a2*x+b2 
+0

怎麼樣X1 = 10,Y1 = 20,X2 = 15,Y2 = 20,也不會產生它的錯誤上面的腳本作爲將變爲零,然後@邁克爾Ø – user7064921

+0

我的回答將是相同的作爲@ bibi's:如果你的線條是水平的,並且垂直線條變爲無窮大,請改爲繪製箭頭。 – 2016-11-29 23:48:27

+0

你能幫我解決這個問題嗎? http://stackoverflow.com/questions/40883823/draw-different-colored-regions-in-gnuplot @Michael – user7064921

0

這是簡單的數學:

  1. 您的直線斜率:slope = (y2 - y1)/(x2 - x1)

  2. 您的直線方程:line(x) = slope * (x - x1) + y1

  3. 中間點(稱之爲​​,ym因爲xy保留) :

    xm=(x1+x2)/2.0

    ym=(y1+y2)/2.0

  4. 垂線方程:line_perp(x) = -(x-xm)/slope + ym

  5. 情節兩者:plot line(x), line_perp(x)


萬一y2==y1x2==x1即兩個點都在水平/垂直你可以使用箭頭來修復腳本:

if (y2==y1 || x2==x1) { 
    set arrow from xm, graph 0 to xm, graph 1 nohead 
    plot ym 
} else { 
    plot line(x), line_perp(x) 
} 
+0

只是得到一個單一的線爲x1 = 10。; y1 = 20。; x2 = 15。; y2 = 20 。 (y2-y1)/(x2-x1)*(x-x1)+ y1, - (x2-x1)/(y2-y1)*(x-(x1 + x2)/2.0)+(y1 + y2)/2.0 @bibi – user7064921

+0

我已經更新了答案,以便更完整 – bibi

+0

你能幫我解決這個問題嗎? http://stackoverflow.com/questions/40883823/draw-different-colored-regions-in-gnuplot @bibi – user7064921