# algorithm - applications - computational geometry中文

## 如何計算鏡像點沿線? (4)

``````P=(x1,y1) and y = ax + b
``````

``````P'(x2,y2), with x2 = x1, y2 = y1 + 2(b-y1)
``````

1. 得到與你的直線正交的通用方程：y = cx + d，ac = -1

==> c = -1 / a

2. 確定b使得P屬於正交線：y1 = -x1 / a + d

==> d = y1 + x1 / a

3. 得到兩條線的交點：y = -x / a + y1 + x1 / a = ax + b

==> x =（y1 + x1 / a -b）/（a + 1 / a），y = a（y1 + x1 / a -b）/（a + 1 / a）+ b

4. 得到交點和你的點P之間的矢量，把它加到交點得到鏡像點P'。

（y1 + x1 / a -b）/（a + 1 / a）-x1）y2 = y1 + 2（a1 + x1 / a -b）/（a + 1 / a）+ b-y1）

``````public class Line
{
protected const double epsilon = 1.0e-8;

public Point Anchor { get; set; }

public double Slope { get; set; }

public virtual Point ClosestPoint(Point pt)
{ return Intersection(Make(pt, -1 / Slope)); }

protected Line(Point anchor, double slope)
{
Anchor = anchor;
Slope = slope;
}

public static Line Make(Point anchor, double slope)
{ return new Line(anchor, slope); }

public virtual Point Intersection(Line line)
{
if (lib.Within(line.Slope, Slope, epsilon))
if( lib.Within(line.YIntcpt, YIntcpt, epsilon))
// code for NoInterceptException not included
throw new NoInterceptException(
"The two lines overlap.");
else return Point.NullPoint;
// ----------------------------------------
double tm = Slope, om = line.Slope,
tx = Anchor.X, ty = Anchor.Y,
ox = line.Anchor.X, oy = line.Anchor.Y;

var x = double.IsInfinity(tm) ? tx :
double.IsInfinity(om) ? ox :
(tm * tx - om * ox + oy - ty) /
(tm - om);

var y = double.IsInfinity(tm) ?
om * (x - ox) + oy :
tm * (x - tx) + ty;

return Point.Make(x, y);
}
}

public struct Point
{
const double epsilon = 1.0e-12;
public bool HasValue { get { return isDef; } }
public bool IsNull { get { return !isDef; } }

private Point(double xValue, double yValue)
{ x = xValue; y = yValue; isDef = true; }
public static Point Make(double x, double y)
{ return new Point(x, y); }

public double X
{
get
{
// code for AlgebraicException not included
if (IsNull) throw new AlgebraicException("Null Point Object");
return x;
}
}
public double Y
{
get
{
// code for AlgebraicException not included
if (IsNull) throw new AlgebraicException("Null Point Object");
return y;
}
}

public static Point NullPoint { get { return new Point();}}

//  Other functionality

}
``````

Q '= Q + 2（I - nn T ）（ P - Q

（這裡，I是2×2單位矩陣， n Tn的轉置矩陣（將n視為2×1矩陣）， nn T是由nn T的標準矩陣乘法形成的2×2矩陣。）表明如果你將P移動到任何位置， Q '將不會改變。