private void CurveEnd(PointFP control1, PointFP control2, PointFP curveEnd) { _drawingCurve = false; if (_needDrawStartCap) { _startCapP1 = new PointFP(_curveBegin); _startCapP2 = new PointFP(control1); _needDrawStartCap = false; } var head = new LineFP(); var tail = new LineFP(); CalcHeadTail(_curveBegin, control1, head, new LineFP()); _outline.AddMoveTo(head.Pt1); _outline.AddPath(_curvePath1); CalcHeadTail(control2, curveEnd, new LineFP(), tail); _outline.AddLineTo(tail.Pt1); _outline.AddLineTo(tail.Pt2); _outline.ExtendIfNeeded(_curvePath1._cmdsSize, _curvePath1._pntsSize); int j = _curvePath2._pntsSize - 1; for (int i = _curvePath2._cmdsSize - 1; i >= 0; i--) { _outline.AddLineTo(_curvePath2._pnts[j--]); } _outline.AddLineTo(head.Pt2); _outline.AddClose(); _curvePath1 = null; _curvePath2 = null; _lastCurveTail = null; _lastPoint = new PointFP(control2); _drawingCurve = false; }
public LineMask(PointFP p0, FPInt dw, FPInt dh) { _type = MaskType.Line; _p0 = p0; _w = dw; _h = dh; }
public AABBMask(PointFP p, FPInt width, FPInt height) { _type = MaskType.AABB; _point = p; _w = width; _h = height; }
public LineMask(PointFP p0, PointFP p1) { _type = MaskType.Line; _p0 = p0; _w = p1.X - p0.X; _h = p1.Y - p0.Y; }
private void AddLineCap(PointFP p1, PointFP p2, int lineCap) { if (lineCap == PenFP.LINECAP_BUTT || p1.Equals(p2)) { return; } var dx = p2.X - p1.X; var dy = p2.Y - p1.Y; var len = PointFP.Distance(dx, dy); var cap = lineCap == PenFP.LINECAP_ROUND ? GraphicsPathFP.ROUNDCAP : GraphicsPathFP.SQUARECAP; dx = MathFP.Mul(_ffRad, MathFP.Div(dx, len)); dy = MathFP.Mul(_ffRad, MathFP.Div(dy, len)); var m = new MatrixFP(dx, dx, dy, -dy, p2.X, p2.Y); _outline.AddMoveTo(new PointFP(0, GraphicsPathFP.ONE).Transform(m)); for (var i = 0; i < cap.Length; i++) { _outline.AddLineTo(new PointFP(cap[i]).Transform(m)); } _outline.AddLineTo(new PointFP(0, -GraphicsPathFP.ONE).Transform(m)); _outline.AddClose(); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 15JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Strokes the outline of a IShape using the settings of the current * Graphics2D context. * @param brush the brush used to fill the shape. * @param shape the IShape to be rendered. */ public void Fill(Brush brush, IShape shape) { if (brush != null) { _graphicsFP.SetBrush(brush._wrappedBrushFP); _defaultBrush = brush; } PathIterator pathIterator = shape.GetPathIterator(null); int[] coords = new int[6]; GraphicsPathFP graphicsPathFP = new GraphicsPathFP(); PointFP pointFP1 = new PointFP(); PointFP pointFPCtl1 = new PointFP(); PointFP pointFPCtl2 = new PointFP(); while (!pathIterator.IsDone()) { int type = pathIterator.CurrentSegment(coords); switch (type) { case PathIterator.SEG_MOVETO: pointFP1.Reset(coords[0] << SingleFP.DECIMAL_BITS, coords[1] << SingleFP.DECIMAL_BITS); graphicsPathFP.AddMoveTo(pointFP1); break; case PathIterator.SEG_CLOSE: graphicsPathFP.AddClose(); break; case PathIterator.SEG_LINETO: pointFP1.Reset(coords[0] << SingleFP.DECIMAL_BITS, coords[1] << SingleFP.DECIMAL_BITS); graphicsPathFP.AddLineTo(pointFP1); break; case PathIterator.SEG_QUADTO: pointFPCtl1.Reset(coords[0] << SingleFP.DECIMAL_BITS, coords[1] << SingleFP.DECIMAL_BITS); pointFP1.Reset(coords[2] << SingleFP.DECIMAL_BITS, coords[3] << SingleFP.DECIMAL_BITS); graphicsPathFP.AddQuadTo(pointFPCtl1, pointFP1); break; case PathIterator.SEG_CUBICTO: pointFPCtl1.Reset(coords[0] << SingleFP.DECIMAL_BITS, coords[1] << SingleFP.DECIMAL_BITS); pointFPCtl2.Reset(coords[2] << SingleFP.DECIMAL_BITS, coords[3] << SingleFP.DECIMAL_BITS); pointFP1.Reset(coords[4] << SingleFP.DECIMAL_BITS, coords[5] << SingleFP.DECIMAL_BITS); graphicsPathFP.AddCurveTo(pointFPCtl1, pointFPCtl2, pointFP1); break; } pathIterator.Next(); } _graphicsFP.FillPath(graphicsPathFP); }
public override void MoveTo(PointFP point) { FinishCurrentSegment(); _needDrawStartCap = true; _closed = false; _startCapP1 = _startCapP2 = null; base.MoveTo(point); }
private void CurveBegin(PointFP control) { AddLineJoin(_lastPoint, _currPoint, control); _drawingCurve = true; _curvePath1 = new GraphicsPathFP(); _curvePath2 = new GraphicsPathFP(); _curveBegin = new PointFP(_currPoint); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * return the union of the rectangle and the given point. * @param p * @return */ public RectangleFP Union(PointFP p) { if (!IsEmpty()) { Reset(MathFP.Min(_ffXmin, p.X), MathFP.Max(_ffXmax, p.X), MathFP.Min(_ffYmin, p.Y), MathFP.Max(_ffYmax, p.Y)); } return(this); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Get the head outline. * @param ff_rad * @return */ public LineFP GetHeadOutline(int ffRad) { var p = new PointFP(Pt1.X - Pt2.X, Pt1.Y - Pt2.Y); var len = GetLength(); p.Reset(MathFP.Div(-p.Y, len), MathFP.Div(p.X, len)); p.Reset(MathFP.Mul(p.X, ffRad), MathFP.Mul(p.Y, ffRad)); return(new LineFP(Pt1.X - p.X, Pt1.Y - p.Y, Pt1.X + p.X, Pt1.Y + p.Y)); }
internal static PointFP[] ToPointFPArray(Point[] pnts) { PointFP[] result = new PointFP[pnts.Length]; for (int i = 0; i < pnts.Length; i++) { result[i] = ToPointFP(pnts[i]); } return(result); }
internal PointFP ClosestPoint(PointFP p) { if (p.X <= _x1) { return new PointFP(_x1, _y); } else if (p.X >= _x2) { return new PointFP(_x2, _y); } return new PointFP(p.X, _y); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 15JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Strokes the outline of a IShape using the settings of the current * Graphics2D context. * @param pen the pen used to stroke the shape. * @param shape the IShape to be rendered. */ public void Draw(Pen pen, IShape shape) { SetGraphicsFPPenAttribute(pen); PathIterator pathIterator = shape.GetPathIterator(null); int[] coords = new int[6]; GraphicsPathFP graphicsPathFP = new GraphicsPathFP(); PointFP pointFP1 = new PointFP(); PointFP pointFPCtl1 = new PointFP(); PointFP pointFPCtl2 = new PointFP(); while (!pathIterator.IsDone()) { int type = pathIterator.CurrentSegment(coords); switch (type) { case PathIterator.SEG_MOVETO: pointFP1.Reset(coords[0] << SingleFP.DECIMAL_BITS, coords[1] << SingleFP.DECIMAL_BITS); graphicsPathFP.AddMoveTo(pointFP1); break; case PathIterator.SEG_CLOSE: graphicsPathFP.AddClose(); break; case PathIterator.SEG_LINETO: pointFP1.Reset(coords[0] << SingleFP.DECIMAL_BITS, coords[1] << SingleFP.DECIMAL_BITS); graphicsPathFP.AddLineTo(pointFP1); break; case PathIterator.SEG_QUADTO: pointFPCtl1.Reset(coords[0] << SingleFP.DECIMAL_BITS, coords[1] << SingleFP.DECIMAL_BITS); pointFP1.Reset(coords[2] << SingleFP.DECIMAL_BITS, coords[3] << SingleFP.DECIMAL_BITS); graphicsPathFP.AddQuadTo(pointFPCtl1, pointFP1); break; case PathIterator.SEG_CUBICTO: pointFPCtl1.Reset(coords[0] << SingleFP.DECIMAL_BITS, coords[1] << SingleFP.DECIMAL_BITS); pointFPCtl2.Reset(coords[2] << SingleFP.DECIMAL_BITS, coords[3] << SingleFP.DECIMAL_BITS); pointFP1.Reset(coords[4] << SingleFP.DECIMAL_BITS, coords[5] << SingleFP.DECIMAL_BITS); graphicsPathFP.AddCurveTo(pointFPCtl1, pointFPCtl2, pointFP1); break; } pathIterator.Next(); } _graphicsFP.DrawPath(graphicsPathFP); }
public AABBMask(PointFP p0, PointFP p1) { _type = MaskType.AABB; FPInt minx = (p0.X < p1.X) ? p0.X : p1.X; FPInt miny = (p0.Y < p1.Y) ? p0.Y : p1.Y; _point = new PointFP(minx, miny); _w = FPMath.Abs(p1.X - p0.X); _h = FPMath.Abs(p1.Y - p0.Y); }
private void CalcHeadTail(PointFP p1, PointFP p2, LineFP head, LineFP tail) { var curr = new LineFP(p1, p2); head.Reset(curr.GetHeadOutline(_ffRad)); var dx = p2.X - p1.X; var dy = p2.Y - p1.Y; tail.Reset(head.Pt1.X + dx, head.Pt1.Y + dy, head.Pt2.X + dx, head.Pt2.Y + dy); }
private void AddLineJoin(PointFP lastPoint, PointFP currPoint, PointFP nextPoint) { if (lastPoint == null || currPoint == null || nextPoint == null || nextPoint.Equals(currPoint) || lastPoint.Equals(currPoint)) { return; } PointFP p1 = null, p2 = null; LineFP head, tail, lastHead, lastTail; CalcHeadTail(currPoint, nextPoint, head = new LineFP(), tail = new LineFP()); CalcHeadTail(lastPoint, currPoint, lastHead = new LineFP(), lastTail = new LineFP()); var needLineJoin = false; var pi1 = new PointFP(); var pi2 = new PointFP(); var cross1 = LineFP.Intersects(new LineFP(head.Pt1, tail.Pt1), new LineFP(lastHead.Pt1, lastTail.Pt1), pi1); var cross2 = LineFP.Intersects(new LineFP(head.Pt2, tail.Pt2), new LineFP(lastHead.Pt2, lastTail.Pt2), pi2); if (cross1 && !cross2 && pi1.X != SingleFP.NOT_A_NUMBER) { p1 = lastTail.Pt2; p2 = head.Pt2; needLineJoin = true; } else if (!cross1 && cross2 && pi2.X != SingleFP.NOT_A_NUMBER) { p1 = lastTail.Pt1; p2 = head.Pt1; needLineJoin = true; } if (needLineJoin) { _outline.AddMoveTo(cross1 ? pi1 : pi2); _outline.AddLineTo(cross1 ? p2 : p1); if (_lineJoin == PenFP.LINEJOIN_MITER) { _outline.AddLineTo(cross1 ? pi2 : pi1); } _outline.AddLineTo(cross1 ? p1 : p2); _outline.AddClose(); if (_lineJoin == PenFP.LINEJOIN_ROUND) { AddLineCap(cross2 ? pi2 : pi1, currPoint, PenFP.LINECAP_ROUND); } } }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * get the tail outline. * @param ff_rad * @return */ public LineFP GetTailOutline(int ffRad) { var c = GetCenter(); var p = new PointFP(Pt2.X - c.X, Pt2.Y - c.Y); p.Reset(p.Y, -p.X); var dis = PointFP.Distance(PointFP.Origin, p); if (dis == 0) { dis = 1; } p.Reset(MathFP.Div(MathFP.Mul(p.X, ffRad), dis), MathFP.Div(MathFP.Mul(p.Y, ffRad), dis)); return(new LineFP(Pt2.X - p.X, Pt2.Y - p.Y, Pt2.X + p.X, Pt2.Y + p.Y)); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * * @param distance * @return */ internal PointFP GetPointAtDistance(int distance) { var lineLength = GetLength(); if (distance > lineLength) { return(null); } if (distance == lineLength) { return(new PointFP(Pt2)); } var scale = MathFP.Div(distance, lineLength); var pointFP = new PointFP(); pointFP.Reset(Pt1.X + MathFP.Mul(Pt2.X - Pt1.X, scale), Pt1.Y + MathFP.Mul(Pt2.Y - Pt1.Y, scale)); return(pointFP); }
public override void LineTo(PointFP point) { if (point.Equals(_currPoint)) { return; } LineFP head, tail; CalcHeadTail(_currPoint, point, head = new LineFP(), tail = new LineFP()); if (_drawingCurve) { if (_lastCurveTail != null) { _curvePath1.AddLineTo(_lastCurveTail.Pt1); _curvePath2.AddLineTo(_lastCurveTail.Pt2); } _lastCurveTail = new LineFP(tail); } else { if (_needDrawStartCap) { _startCapP1 = new PointFP(_currPoint); _startCapP2 = new PointFP(point); _needDrawStartCap = false; } AddLineJoin(_lastPoint, _currPoint, point); _outline.AddMoveTo(head.Pt1); _outline.AddLineTo(tail.Pt1); _outline.AddLineTo(tail.Pt2); _outline.AddLineTo(head.Pt2); _outline.AddLineTo(head.Pt1); _outline.AddClose(); _lastPoint = new PointFP(_currPoint); } base.LineTo(point); }
public TriangleMask(PointFP p0, PointFP p1, PointFP p2) { _type = MaskType.Triangle; _p0 = p0; _p1 = p1; _p2 = p2; VectorFP a = (VectorFP)_pos + _p0; VectorFP b = (VectorFP)_pos + _p1; VectorFP c = (VectorFP)_pos + _p2; _det = (b.Y - c.Y) * (a.X - c.X) + (c.X - b.X) * (a.Y - c.Y); //_det = 1 / _det; FPInt minx = FPMath.Min(_p0.X, FPMath.Min(_p1.X, _p2.X)); FPInt maxx = FPMath.Max(_p0.X, FPMath.Max(_p1.X, _p2.X)); FPInt miny = FPMath.Min(_p0.Y, FPMath.Min(_p1.Y, _p2.Y)); FPInt maxy = FPMath.Max(_p0.Y, FPMath.Max(_p1.Y, _p2.Y)); _bound = new RectangleFP(minx, miny, maxx - minx, maxy - miny); }
internal bool IntersectsLine(PointFP c, PointFP d) { // Check that given line is on both sides of AXLine FPLong yf = _x2 - _x1; FPLong t = _y * yf; FPLong f1 = yf * c.Y - t; FPLong f2 = yf * d.Y - t; if (f1 * f2 >= 0) { return false; } // Check that AXLine is on both sides of given line FPLong xf = d.Y - c.Y; t = (d.X - c.X) * _y - (d.X * c.Y - c.X * d.Y); f1 = xf * _x1 - t; f2 = xf * _x2 - t; return (f1 * f2 < 0); }
public static Entity Create(Engine engine, SystemManager sysManager, PointFP location) { RenderSystem renderSys = sysManager.GetSystem(typeof(RenderSystem)) as RenderSystem; if (renderSys == null) return new Entity(); Entity ent = sysManager.World.EntityManager.Create(); Amphibian.Systems.Rendering.Spatial spat = new DebugCueSpatial(); spat.Initialize(engine.Content); Amphibian.Systems.Rendering.SpatialRef potSR = renderSys.SpatialManager.Add(spat); Renderable entRC = new Renderable(); entRC.SpatialRef = potSR; entRC.RenderX = location.X; entRC.RenderY = location.Y; RemovalTimeout entRT = new RemovalTimeout(0.5f); sysManager.World.EntityManager.AddComponent(ent, entRC); sysManager.World.EntityManager.AddComponent(ent, entRT); return ent; }
public override void CurveTo(PointFP control1, PointFP control2, PointFP point) { CurveBegin(control1); base.CurveTo(control1, control2, point); CurveEnd(control1, control2, point); }
public override void QuadTo(PointFP control, PointFP point) { CurveBegin(control); base.QuadTo(control, point); CurveEnd(control, control, point); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Copy constructor. * @param p */ public PointFP(PointFP p) { Reset(p); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Constructor. * @param p1 * @param p2 */ public LineFP(PointFP p1, PointFP p2) { Reset(p1, p2); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Add given point the location to this point. * @param p * @return */ public PointFP Add(PointFP p) { Reset(X + p.X, Y + p.Y); return(this); }
public override void Draw(SpriteBatch spriteBatch, PointFP position) { _anims[(int)_currentDirection].Draw(spriteBatch, position); }
internal PointFP ClosestPoint(PointFP p) { VectorFP a = (VectorFP)_pos + _p0; VectorFP b = (VectorFP)_pos + _p1; VectorFP c = (VectorFP)_pos + _p2; // Check if P in region outside A VectorFP ab = b - a; VectorFP ac = c - a; VectorFP ap = p - a; FPInt d1 = VectorFP.Dot(ab, ap); FPInt d2 = VectorFP.Dot(ac, ap); if (d1 <= 0 && d2 <= 0) { return a; } // Check if P in region outside B VectorFP bp = p - b; FPInt d3 = VectorFP.Dot(ab, bp); FPInt d4 = VectorFP.Dot(ac, bp); if (d3 >= 0 && d4 <= d3) { return b; } // Check if P in edge region of AB FPInt vc = d1 * d4 - d3 * d2; if (vc <= 0 && d1 >= 0 && d3 <= 0) { FPInt v = d1 / (d1 - d3); return a + v * ab; } // Check if P in region outside C VectorFP cp = p - c; FPInt d5 = VectorFP.Dot(ab, cp); FPInt d6 = VectorFP.Dot(ac, cp); if (d6 >= 0 && d5 <= d6) { return c; } // Check if P in edge region of AC FPInt vb = d5 * d2 - d1 * d6; if (vb <= 0 && d2 >= 0 && d6 <= 0) { FPInt w = d2 / (d2 - d6); return a + w * ac; } // Check if P in edge region of BC FPInt va = d3 * d6 - d5 * d4; if (va <= 0 && (d4 - d3) >= 0 && (d5 - d6) >= 0) { FPInt w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); return b + w * (c - b); } // P inside region return p; }
/*public override TestResult TestOverlapExt (Mask mask) { switch (mask._type) { case MaskType.Point: return CollisionTR.TestOverlap(mask as PointMask, this); case MaskType.Line: return CollisionTR.TestOverlap(mask as LineMask, this); case MaskType.Circle: return CollisionTR.TestOverlap(mask as CircleMask, this); case MaskType.AABB: return CollisionTR.TestOverlap(mask as AABBMask, this); case MaskType.Triangle: return CollisionTR.TestOverlap(this, mask as TriangleMask); } return TestResult.None; } public TestResult TestOverlapExt (PointMask mask) { return CollisionTR.TestOverlap(mask, this); } public TestResult TestOverlapExt (LineMask mask) { return CollisionTR.TestOverlap(mask, this); } public TestResult TestOverlapExt (CircleMask mask) { return CollisionTR.TestOverlap(mask, this); } public TestResult TestOverlapExt (AABBMask mask) { return CollisionTR.TestOverlap(mask, this); } public TestResult TestOverlapExt (TriangleMask mask) { return CollisionTR.TestOverlap(this, mask); }*/ internal PointFP Barycentric(PointFP p) { //Vector2 a = _pos + _p0; //Vector2 b = _pos + _p1; //Vector2 c = _pos + _p2; PointFP a = _p0; PointFP b = _p1; PointFP c = _p2; p = (VectorFP)p - _pos; FPInt v = (b.Y - c.Y) * (p.X - c.X) + (c.X - b.X) * (p.Y - c.Y); FPInt u = (c.Y - a.Y) * (p.X - c.X) + (a.X - c.X) * (p.Y - c.Y); return new PointFP(v / _det, u / _det); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * fill a polygon. * @param points */ public void FillPolygon(PointFP[] points) { FillPath(GraphicsPathFP.CreatePolygon(points)); }
/*public override TestResult TestOverlapExt (Mask mask) { return TestResult.None; }*/ internal PointFP ClosestPoint(PointFP p) { FPInt py = _pos.Y + _p.Y; FPInt px1 = _pos.X + _p.X; FPInt px2 = px1 + _w; if (p.X <= px1) { return _p; } else if (p.X >= px2) { return new PointFP(px2, py); } return new PointFP(p.X, py); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * the distance between 2 points. * @param p1 * @param p2 * @return */ static public int Distance(PointFP p1, PointFP p2) { return(Distance(p1.X - p2.X, p1.Y - p2.Y)); }
internal bool IntersectsLineEdge(PointFP c, PointFP d) { FPInt py = _pos.Y + _p.Y; FPInt px1 = _pos.X + _p.X; FPInt px2 = px1 + _w; // Check that given line is on both sides of AXLine FPLong yf = px2 - px1; FPLong t = py * yf; FPLong f1 = yf * c.Y - t; FPLong f2 = yf * d.Y - t; if (f1 * f2 > 0) { return false; } // Check that AXLine is on both sides of given line FPLong xf = d.Y - c.Y; t = (d.X - c.X) * py - (d.X * c.Y - c.X * d.Y); f1 = xf * px1 - t; f2 = xf * px2 - t; return (f1 * f2 <= 0); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * substract given distance (x,y) to this point. * @param p * @return */ public PointFP Sub(PointFP p) { Reset(X - p.X, Y - p.Y); return(this); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * get the lenght of the line. */ public int GetLength() { return(PointFP.Distance(Pt1, Pt2)); }
public CircleMask(PointFP center, FPInt radius) { _type = MaskType.Circle; _p = center; _radius = radius; }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * check to see if two line intects and return the the intersction point. * @param l1 * @param l2 * @param intersection * @return */ public static bool Intersects(LineFP l1, LineFP l2, PointFP intersection) { var x = SingleFP.NOT_A_NUMBER; var y = SingleFP.NOT_A_NUMBER; if (intersection != null) { intersection.Reset(x, y); } var ax0 = l1.Pt1.X; var ax1 = l1.Pt2.X; var ay0 = l1.Pt1.Y; var ay1 = l1.Pt2.Y; var bx0 = l2.Pt1.X; var bx1 = l2.Pt2.X; var by0 = l2.Pt1.Y; var by1 = l2.Pt2.Y; var adx = (ax1 - ax0); var ady = (ay1 - ay0); var bdx = (bx1 - bx0); var bdy = (by1 - by0); if (IsZero(adx) && IsZero(bdx)) { return(IsEqual(ax0, bx0)); } if (IsZero(ady) && IsZero(bdy)) { return(IsEqual(ay0, by0)); } if (IsZero(adx)) { // A vertical x = ax0; y = IsZero(bdy) ? by0 : MathFP.Mul(MathFP.Div(bdy, bdx), x - bx0) + by0; } else if (IsZero(bdx)) { // B vertical x = bx0; y = IsZero(ady) ? ay0 : MathFP.Mul(MathFP.Div(ady, adx), x - ax0) + ay0; } else if (IsZero(ady)) { y = ay0; x = MathFP.Mul(MathFP.Div(bdx, bdy), y - by0) + bx0; } else if (IsZero(bdy)) { y = by0; x = MathFP.Mul(MathFP.Div(adx, ady), y - ay0) + ax0; } else { var xma = MathFP.Div(ady, adx); // slope segment A var xba = ay0 - (MathFP.Mul(ax0, xma)); // y intercept of segment A var xmb = MathFP.Div(bdy, bdx); // slope segment B var xbb = by0 - (MathFP.Mul(bx0, xmb)); // y intercept of segment B // parallel lines? if (xma == xmb) { // Need trig functions return(xba == xbb); } // Calculate points of intersection // At the intersection of line segment A and B, //XA=XB=XINT and YA=YB=YINT x = MathFP.Div((xbb - xba), (xma - xmb)); y = (MathFP.Mul(xma, x)) + xba; } // After the point or points of intersection are calculated, each // solution must be checked to ensure that the point of intersection lies // on line segment A and B. var minxa = MathFP.Min(ax0, ax1); var maxxa = MathFP.Max(ax0, ax1); var minya = MathFP.Min(ay0, ay1); var maxya = MathFP.Max(ay0, ay1); var minxb = MathFP.Min(bx0, bx1); var maxxb = MathFP.Max(bx0, bx1); var minyb = MathFP.Min(by0, by1); var maxyb = MathFP.Max(by0, by1); if (intersection != null) { intersection.Reset(x, y); } return((x >= minxa) && (x <= maxxa) && (y >= minya) && (y <= maxya) && (x >= minxb) && (x <= maxxb) && (y >= minyb) && (y <= maxyb)); }
public static Point ToPoint(PointFP pnt) { return(new Point(SingleFP.ToInt(pnt.X), SingleFP.ToInt(pnt.Y))); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * reset the line the to given points. * @param p1 * @param p2 */ public void Reset(PointFP p1, PointFP p2) { Pt1.Reset(p1); Pt2.Reset(p2); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Check to see if the point is empty one. * @param p * @return */ public static bool IsEmpty(PointFP p) { return(Empty.Equals(p)); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * reset the point to the same location as the given point. * @param p * @return */ public PointFP Reset(PointFP p) { return(Reset(p.X, p.Y)); }
/*public override TestResult TestOverlapExt (Mask mask) { switch (mask._type) { case MaskType.Point: return CollisionTR.TestOverlap(mask as PointMask, this); case MaskType.Line: return CollisionTR.TestOverlap(mask as LineMask, this); case MaskType.Circle: return CollisionTR.TestOverlap(mask as CircleMask, this); case MaskType.AABB: return CollisionTR.TestOverlap(this, mask as AABBMask); case MaskType.Triangle: return CollisionTR.TestOverlap(this, mask as TriangleMask); } return TestResult.None; } public TestResult TestOverlapExt (PointMask mask) { return CollisionTR.TestOverlap(mask, this); } public TestResult TestOverlapExt (LineMask mask) { return CollisionTR.TestOverlap(mask, this); } public TestResult TestOverlapExt (CircleMask mask) { return CollisionTR.TestOverlap(mask, this); } public TestResult TestOverlapExt (AABBMask mask) { return CollisionTR.TestOverlap(this, mask); } public TestResult TestOverlapExt (TriangleMask mask) { return CollisionTR.TestOverlap(this, mask); }*/ internal PointFP ClosestPoint(PointFP p) { VectorFP r = (VectorFP)_pos + _point; PointFP q = p; if (q.X <= r.X) { q.X = r.X; } else { FPInt mx = r.X + _w; if (q.X >= mx) { q.X = mx; } } if (q.Y <= r.Y) { q.Y = r.Y; } else { FPInt my = r.Y + _h; if (q.Y >= my) { q.Y = my; } } return q; }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 13JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Check to see if this rectangle contains given point. * @param p * @return */ public bool Contains(PointFP p) { return(_ffXmin <= p.X && p.X <= _ffXmax && _ffYmin <= p.Y && p.Y <= _ffYmax); }
/*internal TestResult LineIntersect (PointFP p0, PointFP p1) { VectorFP r = (VectorFP)_pos + _point; FPInt rw = r.X + _w; FPInt rh = r.Y + _h; if (p0.X == p1.X) { // Parallel to Y if (p0.X < r.X || p0.X > r.X + _w) { return TestResult.None; } FPInt pmin = FPMath.Min(p0.Y, p1.Y); FPInt pmax = p0.Y + p1.Y - pmin; if (pmin > rh || pmax < r.Y) { return TestResult.None; } if (p0.X == r.X || p0.X == rw) { return TestResult.Overlapping | TestResult.Edge; } if (pmin == rh || pmax == r.Y) { return TestResult.Overlapping | TestResult.Edge; } return TestResult.Overlapping; } else if (p0.Y == p1.Y) { // Parallel to X if (p0.Y < r.Y || p0.Y > rh) { return TestResult.None; } FPInt pmin = FPMath.Min(p0.X, p1.X); FPInt pmax = p0.X + p1.X - pmin; if (pmin > rw || pmax < r.X) { return TestResult.None; } if (p0.Y == r.Y || p0.Y == rh) { return TestResult.Overlapping | TestResult.Edge; } if (pmin == rw || pmax == r.X) { return TestResult.Overlapping | TestResult.Edge; } return TestResult.Overlapping; } FPInt m = (p0.Y - p1.Y) / (p0.X - p1.X); FPInt c = p0.Y - (m * p0.X); FPInt topIntersect = c; FPInt botIntersect = c; if (m > 0) { // Slope descending left to right topIntersect += (m * r.X); // t = mL + c botIntersect += (m * rw); // b = mR + c } else { // Slope descending right to left topIntersect += (m * rw); // t = mR + c botIntersect += (m * r.X); // b = mL + c } FPInt topTriPoint = p0.Y; FPInt botTriPoint = p1.Y; if (p0.Y > p1.Y) { // P0 is actually lower topTriPoint = p1.Y; botTriPoint = p0.Y; } FPInt topOver = (topIntersect > topTriPoint) ? topIntersect : topTriPoint; FPInt botOver = (botIntersect < botTriPoint) ? botIntersect : botTriPoint; // Test ranges if (topOver > botOver || botOver < r.Y || topOver > rh) { return TestResult.None; } // Test edges if (topOver == botOver || botOver == r.Y || topOver == rh) { return TestResult.Overlapping | TestResult.Edge; } return TestResult.Overlapping; }*/ internal bool IntersectsLine(PointFP p0, PointFP p1) { VectorFP r = (VectorFP)_pos + _point; FPInt rw = r.X + _w; FPInt rh = r.Y + _h; if (p0.X == p1.X) { // Parallel to Y if (p0.X < r.X || p0.X > r.X + _w) { return false; } FPInt pmin = FPMath.Min(p0.Y, p1.Y); FPInt pmax = p0.Y + p1.Y - pmin; return !(pmin > rh || pmax < r.Y); } else if (p0.Y == p1.Y) { // Parallel to X if (p0.Y < r.Y || p0.Y > rh) { return false; } FPInt pmin = FPMath.Min(p0.X, p1.X); FPInt pmax = p0.X + p1.X - pmin; return !(pmin > rw || pmax < r.X); } FPInt m = (p0.Y - p1.Y) / (p0.X - p1.X); FPInt c = p0.Y - (m * p0.X); FPInt topIntersect = c; FPInt botIntersect = c; if (m > 0) { // Slope descending left to right topIntersect += (m * r.X); // t = mL + c botIntersect += (m * rw); // b = mR + c } else { // Slope descending right to left topIntersect += (m * rw); // t = mR + c botIntersect += (m * r.X); // b = mL + c } FPInt topTriPoint = p0.Y; FPInt botTriPoint = p1.Y; if (p0.Y > p1.Y) { // P0 is actually lower topTriPoint = p1.Y; botTriPoint = p0.Y; } FPInt topOver = (topIntersect > topTriPoint) ? topIntersect : topTriPoint; FPInt botOver = (botIntersect < botTriPoint) ? botIntersect : botTriPoint; return !(topOver > botOver || botOver < r.Y || topOver > rh); }
public static PointF ToPointF(PointFP pnt) { return(new PointF(SingleFP.ToFloat(pnt.X), SingleFP.ToFloat(pnt.Y))); }
public AABBMask(PointFP[] pointFP) : this(pointFP[0],pointFP[1]) { }
public AXLineMask(PointFP p, FPInt w) { _type = MaskType.AXLine; if (w > 0) { _p = p; _w = w; } else { _p = new PointFP(p.X - w, p.Y); _w = 0 - w; } }
public override void Draw(SpriteBatch spriteBatch, PointFP position) { _definition.Draw(spriteBatch, position, SpriteInfo); }
public AXLine(PointFP origin, FPInt length) { _y = origin.Y; _x1 = origin.X; _x2 = origin.X + length; }