0

Say, I have two points (x1,y1) and (x2,y2) on the same line . The midpoint of joining this two points is (x,y). Is it possible to draw a perpendicular bisector through (x,y) in gnuplot? How will I draw it?

3 Answers3

0

It's simple math:

  1. your line slope: slope = (y2 - y1) / (x2 - x1)

  2. your line equation: line(x) = slope * (x - x1) + y1

  3. the middle point (calling it xm,ym because x,y are reserved):

    xm=(x1+x2)/2.0

    ym=(y1+y2)/2.0

  4. the perpendicular line equation: line_perp(x) = -(x-xm)/slope + ym

  5. plot both: plot line(x), line_perp(x)


In case y2==y1 or x2==x1 i.e the two points are in horizontal/vertical line you can fix the script using an arrow:

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)
}
bibi
  • 3,671
  • 5
  • 34
  • 50
  • just getting a single line for x1=10.;y1=20.;x2=15.;y2=20. plot (y2-y1)/(x2-x1)*(x-x1)+y1, -(x2-x1)/(y2-y1)*(x-(x1+x2)/2.0)+(y1+y2)/2.0 @bibi –  Nov 29 '16 at 21:18
  • can you help me in this question? http://stackoverflow.com/questions/40883823/draw-different-colored-regions-in-gnuplot @bibi –  Nov 30 '16 at 09:04
0

Instead of going via y = a*x + b as in @bibi's and @user4489658's answers, where a could be infinite, I would go via the angle (check help atan2). This covers vertical lines without extra treatment.

Make sure to use set size ratio -1 (check help size) that the perpendicular bisector really looks perpendicular in the graph.

Script:

### drawing perpendicular bisectors
reset session

$Data <<EOD
# x1  y1  x2  y2
 10   20  15  20
 10   10  20  20
 20   10  20  15
 12   10  18  12
 10   12  12  18
EOD

set key out
set offset 1,1,1,1
set angle degrees
set size ratio -1

colX1 = 1
colY1 = 2
colX2 = 3
colY2 = 4
dx(n)   = column(colX2)-column(colX1)
dy(n)   = column(colY2)-column(colY1)
a0(n)   = atan2(dy(n),dx(n)) + 90
xm(n)   = (column(colX1)+column(colX2))*0.5
ym(n)   = (column(colY1)+column(colY2))*0.5
Scaling = 0.2
L0(n)   = Scaling*sqrt(dx(n)**2 + dy(n)**2)
getParams(n) = (dx0=L0(0)*cos(a0(0)), dy0=L0(0)*sin(a0(0)), x0=xm(0)-dx0, y0=ym(0)-dy0)

plot $Data u colX1:colY1 w p pt 7 lc "blue" ti "Start", \
        '' u colX2:colY2 w p pt 7 lc "red"  ti "End", \
        '' u colX1:colY1:(dx(0)):(dy(0)) w vec lc "web-green" filled ti "Vector", \
        '' u (getParams(0),x0):(y0):(2*dx0):(2*dy0) w vec lc "black" dt 3 nohead ti "\nperpendicular\nbisector" 
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
-1

Equation of your line is

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

Midpoint:

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

Equation of perpendicular line:

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

gnuplot script:

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
  • what about x1=10, y1=20, x2=15,y2=20, won't it generate error for the above script as a will become zero then @Michael O –  Nov 29 '16 at 21:11
  • My answer will be the same as @bibi's: if your line is horizontal and the perpendicular line goes to infinity, plot the arrow instead. –  Nov 29 '16 at 23:48
  • can you help me in this question? http://stackoverflow.com/questions/40883823/draw-different-colored-regions-in-gnuplot @Michael –  Nov 30 '16 at 09:04