/// <summary> /// Compute the projection of a point onto the line determined /// by this line segment. /// Note that the projected point may lie outside the line segment. /// If this is the case, the projection factor will lie outside the range [0.0, 1.0]. /// </summary> public static Point Project(Point p, Point lineSegFrom, Point lineSegTo) { if (p.Equals(lineSegFrom) || p.Equals(lineSegTo)) return new Point(p.X, p.Y); var r = ProjectionFactor(p, lineSegFrom, lineSegTo); var coord = new Point { X = lineSegFrom.X + r * (lineSegTo.X - lineSegFrom.X), Y = lineSegFrom.Y + r * (lineSegTo.Y - lineSegFrom.Y) }; return coord; }
public static void Main(string[] args) { var x = new Point(1, 2); var y = x; Console.WriteLine(y); // x のメンバー毎コピー = (1, 2) // メンバー毎比較(全メンバー一致なら一致) Console.WriteLine(x.Equals(new Point(1, 2))); // true Console.WriteLine(x.Equals(new Point(1, 3))); // false }
/// <summary> /// Computes the distance from a line segment AB to a line segment CD. /// Note: NON-ROBUST! /// </summary> /// <param name="a">A point of one line.</param> /// <param name="b">The second point of the line (must be different to A).</param> /// <param name="c">One point of the line.</param> /// <param name="d">Another point of the line (must be different to A).</param> /// <returns>The distance from line segment AB to line segment CD.</returns> public static double DistanceLineLine(Point a, Point b, Point c, Point d) { // check for zero-length segments if (a.Equals(b)) return DistancePointLine(a, c, d); if (c.Equals(d)) return DistancePointLine(d, a, b); // AB and CD are line segments /* from comp.graphics.algo Solving the above for r and s yields (Ay-Cy)(Dx-Cx)-(Ax-Cx)(Dy-Cy) r = ----------------------------- (eqn 1) (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx) (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay) s = ----------------------------- (eqn 2) (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx) Let Point be the position vector of the intersection point, then Point=A+r(B-A) or Px=Ax+r(Bx-Ax) Py=Ay+r(By-Ay) By examining the values of r & s, you can also determine some other limiting conditions: If 0<=r<=1 & 0<=s<=1, intersection exists r<0 or r>1 or s<0 or s>1 line segments do not intersect If the denominator in eqn 1 is zero, AB & CD are parallel If the numerator in eqn 1 is also zero, AB & CD are collinear. */ var rTop = (a.Y - c.Y)*(d.X - c.X) - (a.X - c.X)*(d.Y - c.Y); var rBottom = (b.X - a.X)*(d.Y - c.Y) - (b.Y - a.Y)*(d.X - c.X); var sTop = (a.Y - c.Y)*(b.X - a.X) - (a.X - c.X)*(b.Y - a.Y); var sBottom = (b.X - a.X)*(d.Y - c.Y) - (b.Y - a.Y)*(d.X - c.X); // ReSharper disable CompareOfFloatsByEqualityOperator if ((rBottom == 0) || (sBottom == 0)) return Math.Min(DistancePointLine(a, c, d), Math.Min(DistancePointLine(b, c, d), Math.Min(DistancePointLine(c, a, b), DistancePointLine(d, a, b)))); // ReSharper restore CompareOfFloatsByEqualityOperator var s = sTop/sBottom; var r = rTop/rBottom; if ((r < 0) || (r > 1) || (s < 0) || (s > 1)) //no intersection return Math.Min(DistancePointLine(a, c, d), Math.Min(DistancePointLine(b, c, d), Math.Min(DistancePointLine(c, a, b), DistancePointLine(d, a, b)))); return 0.0; //intersection exists }
/// <summary> /// Computes the distance from a line segment AB to a line segment CD. /// Note: NON-ROBUST! /// </summary> /// <param name="A">A point of one line.</param> /// <param name="B">The second point of the line (must be different to A).</param> /// <param name="C">One point of the line.</param> /// <param name="D">Another point of the line (must be different to A).</param> /// <returns>The distance from line segment AB to line segment CD.</returns> public static double DistanceLineLine(Point A, Point B, Point C, Point D) { // check for zero-length segments if (A.Equals(B)) return DistancePointLine(A, C, D); if (C.Equals(D)) return DistancePointLine(D, A, B); // AB and CD are line segments /* from comp.graphics.algo Solving the above for r and s yields (Ay-Cy)(Dx-Cx)-(Ax-Cx)(Dy-Cy) r = ----------------------------- (eqn 1) (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx) (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay) s = ----------------------------- (eqn 2) (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx) Let Point be the position vector of the intersection point, then Point=A+r(B-A) or Px=Ax+r(Bx-Ax) Py=Ay+r(By-Ay) By examining the values of r & s, you can also determine some other limiting conditions: If 0<=r<=1 & 0<=s<=1, intersection exists r<0 or r>1 or s<0 or s>1 line segments do not intersect If the denominator in eqn 1 is zero, AB & CD are parallel If the numerator in eqn 1 is also zero, AB & CD are collinear. */ double r_top = (A.Y - C.Y) * (D.X - C.X) - (A.X - C.X) * (D.Y - C.Y); double r_bot = (B.X - A.X) * (D.Y - C.Y) - (B.Y - A.Y) * (D.X - C.X); double s_top = (A.Y - C.Y) * (B.X - A.X) - (A.X - C.X) * (B.Y - A.Y); double s_bot = (B.X - A.X) * (D.Y - C.Y) - (B.Y - A.Y) * (D.X - C.X); if ((r_bot == 0) || (s_bot == 0)) return Math.Min(DistancePointLine(A, C, D), Math.Min(DistancePointLine(B, C, D), Math.Min(DistancePointLine(C, A, B), DistancePointLine(D, A, B)))); double s = s_top / s_bot; double r = r_top / r_bot; if ((r < 0) || (r > 1) || (s < 0) || (s > 1)) //no intersection return Math.Min(DistancePointLine(A, C, D), Math.Min(DistancePointLine(B, C, D), Math.Min(DistancePointLine(C, A, B), DistancePointLine(D, A, B)))); return 0.0; //intersection exists }
public void TestEquality() { // Setup. var p1 = new Point(1, 0); var p2 = new Point(1, 0); var p3 = new Point(0, 1); // Dissimilar objects. var s = "Test"; Assert.IsFalse(p1.Equals(s)); // Null object. Assert.IsFalse(p1.Equals((object) null)); // Null point. Assert.IsFalse(p1.Equals(null)); // Point as object equals. Assert.IsTrue(p1.Equals((object) p2)); Assert.IsTrue(p2.Equals((object) p1)); Assert.IsFalse(p3.Equals((object) p1)); // Point as point equals. Assert.IsTrue(p1.Equals(p2)); Assert.IsTrue(p2.Equals(p1)); Assert.IsFalse(p3.Equals(p1)); // Reference equals. Assert.IsTrue(p1 == p1); // Null checks. Assert.IsFalse(null == p1); Assert.IsFalse(p1 == null); // Two different objects. Assert.IsTrue(p1 == p2); // Inequality operator. Assert.IsTrue(p1 != p3); }
/// <summary> /// Compute the projection factor for the projection of the point p /// onto this <c>LineSegment</c>. The projection factor is the constant k /// by which the vector for this segment must be multiplied to /// equal the vector for the projection of p. /// </summary> /// <returns></returns> public static double ProjectionFactor(Point p, Point lineSegFrom, Point lineSegTo) { if (p.Equals(lineSegFrom)) return 0.0; if (p.Equals(lineSegTo)) return 1.0; // Otherwise, use comp.graphics.algorithms Frequently Asked Questions method /* AC dot AB r = ------------ ||AB||^2 r has the following meaning: r=0 Point = A r=1 Point = B r<0 Point is on the backward extension of AB r>1 Point is on the forward extension of AB 0<r<1 Point is interior to AB */ var dx = lineSegTo.X - lineSegFrom.X; var dy = lineSegTo.Y - lineSegFrom.Y; var len2 = dx * dx + dy * dy; var r = ((p.X - lineSegFrom.X) * dx + (p.Y - lineSegFrom.Y) * dy) / len2; return r; }
public override bool IsValidMove(Point target) { Point[] jiugong = this.Side.sideType == SideType.Bottom ? Board.BottomJiuGong : Board.TopJiuGong; if (!jiugong.Contains(target)) return false; if (target.Equals(this.Position)) return false; // * can horizontally or vertically move 1 position // * the target position must NOT be under attack if (((target.X == this.Position.X && Math.Abs(target.Y - this.Position.Y) == 1) || (target.Y == this.Position.Y && Math.Abs(target.X - this.Position.X) == 1)) && !this.Side.Opponent.PointInAttackRange(target)) return true; //otherwise... return false; }
public void CheckLinesTest() { Board b = new Board(7, 7, 3); Point p, pc; List<Point> result; List<Point> knownh, knownv, knowndl, knowndr, known; pc = new Point(2, 2); knownh = new List<Point>(); ushort row; ushort col; ushort count; /* o o o o o o o o * o o o o o o o o * + + + + o o o o * o o o o o o o o * o o o o o o o o */ for (row = pc.Row, col = 0, count = 0; count < 4; col++, count++) { p = new Point(row, col); knownh.Add(p); b.PlaceItem(p, 1); } result = b.CheckLines(knownh.ElementAt(0)); Assert.AreEqual(0, result.Count); /* o o o o o o o o * o o o o o o o o * + + + + + o o o * o o o o o o o o * o o o o o o o o */ p = new Point(row, col++); knownh.Add(p); b.PlaceItem(p, 1); // from right result = b.CheckLines(knownh.ElementAt(4)); CollectionAssert.AreEquivalent(knownh, result); // from middle result = b.CheckLines(knownh.ElementAt(2)); CollectionAssert.AreEquivalent(knownh, result); // from left result = b.CheckLines(knownh.ElementAt(0)); CollectionAssert.AreEquivalent(knownh, result); // switch out the color b.ClearItem(pc); b.PlaceItem(pc, 2); // from left result = b.CheckLines(knownh.ElementAt(0)); Assert.AreEqual(0, result.Count); // from middle result = b.CheckLines(knownh.ElementAt(2)); Assert.AreEqual(0, result.Count); // from right result = b.CheckLines(knownh.ElementAt(4)); Assert.AreEqual(0, result.Count); b.ClearItem(pc); b.PlaceItem(pc, 1); // more than 5 /* o o o o o o o o * o o o o o o o o * + + + + + + o o * o o o o o o o o * o o o o o o o o */ p = new Point(row, col++); knownh.Add(p); b.PlaceItem(p, 1); result = b.CheckLines(p); CollectionAssert.AreEquivalent(knownh, result); /* o o + o o o o o * o o + o o o o o * + + + + + + + o * o o + o o o o o * o o + o o o o o */ knownv = new List<Point>(); for (row = 0, col = pc.Col, count = 0; count < 5; row++, count++) { p = new Point(row, col); knownv.Add(p); if (!p.Equals(pc)) b.PlaceItem(p, 1); } // from bottom result = b.CheckLines(p); CollectionAssert.AreEquivalent(knownv, result); // from top result = b.CheckLines(knownv.ElementAt(0)); CollectionAssert.AreEquivalent(knownv, result); // two directions known = new List<Point>(); known.AddRange(knownh.AsEnumerable()); known.AddRange(knownv.AsEnumerable()); // there is an extra point at the intersection known.Remove(pc); result = b.CheckLines(pc); CollectionAssert.AreEquivalent(known, result); /* o o + o o o o o * o o + o o o o o * + + + + + + + o * o o + + o o o o * o o + o + o o o * o o o o o + o o * o o o o o o + o */ knowndl = new List<Point>(); for (row = pc.Row, col = pc.Col, count = 0; count < 5; row++, col++, count++) { p = new Point(row, col); knowndl.Add(p); if (!p.Equals(pc)) b.PlaceItem(p, 1); } // from top left (second) result = b.CheckLines(knowndl.ElementAt(1)); CollectionAssert.AreEquivalent(knowndl, result); // from bottom right result = b.CheckLines(knowndl.ElementAt(4)); CollectionAssert.AreEquivalent(knowndl, result); // three directions known.AddRange(knowndl.AsEnumerable()); known.Remove(pc); result = b.CheckLines(pc); CollectionAssert.AreEquivalent(known, result); /* o o + o + o o o * o o + + o o o o * + + + + + + + o * o + + + o o o o * + o + o + o o o * o o + o o + o o * o o + o o o + o */ knowndr = new List<Point>(); for (row = 4, col = 0, count = 0; count < 5; row--, col++, count++) { p = new Point(row, col); knowndr.Add(p); if (!p.Equals(pc)) b.PlaceItem(p, 1); } // from bottom left result = b.CheckLines(knowndr.ElementAt(0)); CollectionAssert.AreEquivalent(knowndr, result); // from top right result = b.CheckLines(knowndr.ElementAt(4)); CollectionAssert.AreEquivalent(knowndr, result); // four directions known.AddRange(knowndr.AsEnumerable()); known.Remove(pc); result = b.CheckLines(pc); CollectionAssert.AreEquivalent(known, result); }
/// <summary> /// Computes the distance from a point p to a line segment AB. /// Note: NON-ROBUST! /// </summary> /// <param name="p">The point to compute the distance for.</param> /// <param name="a">One point of the line.</param> /// <param name="b">Another point of the line (must be different to A).</param> /// <returns> The distance from p to line segment AB.</returns> public static double DistancePointLine(Point p, Point a, Point b) { // if start == end, then use pt distance if (a.Equals(b)) return p.Distance(a); // otherwise use comp.graphics.algorithms Frequently Asked Questions method /*(1) AC dot AB r = --------- ||AB||^2 r has the following meaning: r=0 Point = A r=1 Point = B r<0 Point is on the backward extension of AB r>1 Point is on the forward extension of AB 0<r<1 Point is interior to AB */ var r = ((p.X - a.X)*(b.X - a.X) + (p.Y - a.Y)*(b.Y - a.Y)) / ((b.X - a.X)*(b.X - a.X) + (b.Y - a.Y)*(b.Y - a.Y)); if (r <= 0.0) return p.Distance(a); if (r >= 1.0) return p.Distance(b); /*(2) (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay) s = ----------------------------- Curve^2 Then the distance from C to Point = |s|*Curve. */ var s = ((a.Y - p.Y)*(b.X - a.X) - (a.X - p.X)*(b.Y - a.Y)) / ((b.X - a.X)*(b.X - a.X) + (b.Y - a.Y)*(b.Y - a.Y)); return Math.Abs(s)*Math.Sqrt((b.X - a.X)*(b.X - a.X) + (b.Y - a.Y)*(b.Y - a.Y)); }
private static IEnumerable<Rotation> GetPath(Dictionary<Point, int> d, Point from, Point to) { var res = new List<Rotation>(); while (!to.Equals(from)) { var i = d[to] - 1; var nf = to.NearFour(); int j; for (j = 0; j < nf.Length; j++) { if (d.ContainsKey(nf[j]) && d[nf[j]] == i) { res.Add((Rotation)j); to = nf[j]; break; } } if (j == 4) throw new Exception(); } var ress = res.ToArray(); ReversePath(ress); return ress; }
public Line(Point beg, Point end) { double l = beg.Distance(end); if (FP.eq(l, Math.PI)) { Debug.Assert(FP.eq(beg.ra, end.ra)); phi = -Math.PI/2; theta = Math.PI/2; psi = beg.ra < 0.0 ? Math.PI*2 + beg.ra : beg.ra; length = Math.PI; return; } if (beg.Equals(end)) { phi = Math.PI/2; theta = beg.dec; psi = beg.ra - Math.PI/2; length = 0.0; } else { Point3D beg3d = new Point3D(beg); Point3D end3d = new Point3D(end); Point3D tp = new Point3D(); Point spt = beg3d.cross(end3d).toSpherePoint(); Euler euler = new Euler(); euler.phi = - spt.ra - Math.PI/2; euler.theta = spt.dec - Math.PI/2; euler.psi = 0.0 ; euler.psi_a = Euler.AXIS_Z; euler.theta_a = Euler.AXIS_X; euler.phi_a = Euler.AXIS_Z; euler.transform(tp, beg3d); spt = tp.toSpherePoint(); // invert phi = spt.ra; theta = -euler.theta; psi = -euler.phi; length = l; } }
public void Equals(Point a, Point b, bool result) { (a == b).Should().Be(result); (a.Equals(b)).Should().Be(result); }
/// <summary> /// Compute the projection of a point onto the line determined /// by this line segment. /// Note that the projected point may lie outside the line segment. /// If this is the case, the projection factor will lie outside the range [0.0, 1.0]. /// </summary> /// <param name="p"></param> /// <returns></returns> public static Point Project(Point p, Point LineSegFrom, Point LineSegTo) { if (p.Equals(LineSegFrom) || p.Equals(LineSegTo)) return new Point(p.X, p.Y); var r = ProjectionFactor(p, LineSegFrom, LineSegTo); Point coord = new Point { X = LineSegFrom.X + r * (LineSegTo.X - LineSegFrom.X), Y = LineSegFrom.Y + r * (LineSegTo.Y - LineSegFrom.Y) }; return coord; }
public void TestPointEquals () { var point = new Point (2, 4); Assert.True (point.Equals (new Point (2, 4))); Assert.False (point.Equals (new Point (3, 4))); Assert.False (point.Equals ("Point")); }
public List<Point> GetWarpingPath(int i, int j) { List<Point> bSeq = new List<Point>(); Point p = new Point(i, j); while ((!p.Equals(Point.Empty)) && (!p.Equals(new Point(0,0)))) { bSeq.Insert(0, p); p = MinimalPath[(int)p.X, (int)p.Y]; //when reaching end, stop procedure if (p.Equals(new Point(0,0))) { //insert point <0,0> bSeq.Insert(0, p); break; } } return bSeq; }