I have a line segment (x1, y1), (x2, y2) that i need "mirrored" so the new line becomes perpendicular to the first one and passes through its middle.
The lin开发者_Python百科e segment (0,0),(2,2) should return a new line segment (0,2),(2,0)
Can anyone help me with a function/formula to handle this?
The midpoint is (mx,my) = ((x1+x2)/2,(y1+y2)/2).
To rotate the endpoints 90 degrees about the middle, first compute a vector:
(dx,dy) = (x1-mx),(y1-my)
then rotate it 90 degrees:
dx1 = -dy
dy1 = dx
Then the new point becomes:
x1 = mx+dx1
y1 = my+dy1
Repeat for x2,y2.
You may also combine steps if you're careful.
It seems you are looking for a symmetry. well, a good way of doing that could be using a scaling matrix.
vx, vy, vz are the coefficient of scaling. px,py,pz are the coordinate of a point. If you multiply the scale matrix for the point coordinate, you will get the scaled coordinates of that point.
A scaling matrix with vx=vy=vz= 1 is an identity transform. For obtain a mirror effect you could simply invert the matrix coefficient of the axis on which you want to perform the symmetry.
For example :
1 0 0|px
0 -1 0|py
0 0 1|pz
will calculate the symmetric point of the given vector respect to y axis.
精彩评论