public Vector2Edge(Vector2X Start, Vector2X End, FortuneSite Left, FortuneSite Right) { start = Start; end = End; left = Left; right = Right; }
public void V2_Parse() { Vector2 result = Vector2X.Parse("3.5,-4.2"); result.X.Should().BeApproximately(3.5f, 1e-6f); result.Y.Should().BeApproximately(-4.2f, 1e-6f); }
public void V2_Equals() { Vector2 a = Vector2.UnitX; Vector2 b = Vector2.UnitX * 1.000001f; Vector2X.Equals(a, b, 0.0001f).Should().BeTrue(); }
internal override Vector2X[] adjustUVs(Vector2X[] uv, Vector2X[] result = null) { SubTextureX texture = this; _transform2X.identity(); while (texture != null) { _transform2X = Transform2X.multiply(_transform2X, texture.transform); texture = texture.parent as SubTextureX; } Vector2X item; if (result == null) { result = new Vector2X[4]; } for (int i = 0; i < 4; i++) { item = uv[i]; result[i] = _transform2X.transformVector(item); } return(result); }
public void V2_ParseScientificNotation() { Vector2 result = Vector2X.Parse("-1.2e+12,2.4e-18"); result.X.Should().BeApproximately(-1.2e+12f, 1e-6f); result.Y.Should().BeApproximately(2.4e-18f, 1e-6f); }
public override DisplayObjectX hitTest(Vector2X localVector, bool forTouch = false) { if (forTouch && (visible == false || touchable == false)) { return(null); } float localX = localVector.x; float localY = localVector.y; int numChildren = mChildren.Count; DisplayObjectX child; DisplayObjectX target; Vector2X transformVector; for (int i = numChildren - 1; i >= 0; --i) // front to back! { child = mChildren[i]; getTransformationMatrix(child, sHelperTransform); transformVector = sHelperTransform.transformVector(localVector); target = child.hitTest(transformVector); if (target != null) { return(forTouch && mTouchGroup ? this : target); } } return(null); }
public void V2_RotateEquals() { Vector2 a = new Vector2(3, 4); Vector2 b = a.RotateDegrees(90); var expected = new Vector2(4, -3); Vector2X.Equals(b, expected).Should().BeTrue(); }
public void updateMousePosition(Vector2X mousePos, Vector2X deltaPos) { _mouseX = (int)mousePos.x; _mouseY = (int)mousePos.y; _deltaMouseX = (int)deltaPos.x; _deltaMouseY = (int)deltaPos.y; //string message = string.Format("mx:{0},my:{1},dx:{2},dy:{3}", _mouseX, _mouseY, _deltaMouseX, _deltaMouseY); //Debug.Log(message); }
public void Line_SegmentIntersectionLowSymmetry() { var result = LineAlgorithms.LineSegmentIntersection( new Vector2(704, 336), new Vector2(569, 299), new Vector2(436, 218), new Vector2(446, 357)); result.Should().NotBeNull(); Vector2X.Equals(result.IntersectionPoint, new Vector2(439.269f, 263.444f), 1e-3f).Should().BeTrue( "Intersection was not in correct position."); }
//Point inside convex polygon public static bool InsideConvex(List <Vector2X> polygon, Vector2X point) { //Check if a triangle or higher n-gon if (polygon.Count < 3) { return(false); } //n>2 Keep track of cross product sign changes var pos = 0; var neg = 0; for (var i = 0; i < polygon.Count; i++) { //If point is in the polygon if (polygon[i] == point) { return(true); } //Form a segment between the i'th point var x1 = polygon[i].value.x; var y1 = polygon[i].value.y; //And the i+1'th, or if i is the last, with the first point var i2 = i < polygon.Count - 1 ? i + 1 : 0; var x2 = polygon[i2].value.x; var y2 = polygon[i2].value.y; var x = point.value.x; var y = point.value.y; //Compute the cross product var d = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1); if (d > 0) { pos++; } if (d < 0) { neg++; } //If the sign changes, then point is outside if (pos > 0 && neg > 0) { return(false); } } //If no change in direction, then on same side of all segments, and thus inside return(true); }
private static void ProjectionTest(Vector2 v, Vector2 direction, Vector2 expected) { Vector2 result = v.ProjectionOn(direction); var perp = v - result; Vector2.Dot(direction, perp).Should().BeApproximately(0, 1e-5f); Vector2X.Equals(result, expected).Should().BeTrue( $"Projection of {v} on {direction} should yield {expected} but got {result} instead."); }
public void StarBuild_SixPointedStarCenter() { var center = new Vector2(10, 12); var star = new StarBuilder().BuildStar(6, 8, 4, center, 1); star.Points.Count.Should().Be(12); var avg = star.Points.Average(); Vector2X.Equals(center, avg, 1e-6f).Should().BeTrue($"Expected {center}, actual {avg}"); }
public void Poly_Rotate() { var a = RectangleX.FromLTRB(0, 0, 10, 5).ToPolygon(); var b = a.RotateDegrees(90, new Vector2(10, 5)); b.Count.Should().Be(4); b.Any(x => Vector2X.Equals(x, new Vector2(5, 5), 1e-5f)).Should().BeTrue(); b.Any(x => Vector2X.Equals(x, new Vector2(10, 5), 1e-5f)).Should().BeTrue(); b.Any(x => Vector2X.Equals(x, new Vector2(10, 15), 1e-5f)).Should().BeTrue(); b.Any(x => Vector2X.Equals(x, new Vector2(5, 15), 1e-5f)).Should().BeTrue(); }
public void V2_FromPolar() { Vector2 a = Vector2X.FromPolar(1, MathF.PI * 0.5f); Vector2 b = Vector2X.FromPolar(1, MathF.PI * 1.0f); Vector2 c = Vector2X.FromPolar(1, MathF.PI * 1.5f); Vector2 d = Vector2X.FromPolar(1, MathF.PI * 2.0f); Vector2X.Equals(a, Vector2.UnitY, .000001f); Vector2X.Equals(b, Vector2.UnitX, .000001f); Vector2X.Equals(c, Vector2.UnitY, .000001f); Vector2X.Equals(d, Vector2.UnitX, .000001f); }
//Jitters a line segment of 2 Vector2 public static List <Vector2X> Jitter(Vector2X start, Vector2X end, int divisions, float magnitude) { //Create list and add start List <Vector2X> jitter = new List <Vector2X>(); jitter.Add(start); //Vector float vecX = end.value.x - start.value.x; float vecY = end.value.y - start.value.y; //Distance of points float distance = Mathf.Sqrt(vecX * vecX + vecY * vecY); //Jitter distance float jitterDistance = distance / divisions; //Normalized Vector float dirX = vecX / distance; float dirY = vecY / distance; //Perpendicular float normalX = dirY; float normalY = -dirX; //Division step float divisionX = dirX * jitterDistance; float divisionY = dirY * jitterDistance; //For each division for (int i = 1; i < divisions; i++) { //Begin at first point float finalX = start.value.x; float finalY = start.value.y; //Move N divisions finalX += i * divisionX; finalY += i * divisionY; //Move randomly in perpendicular finalX += UnityEngine.Random.Range(-magnitude, magnitude) * normalX; finalY += UnityEngine.Random.Range(-magnitude, magnitude) * normalY; //Add new point jitter.Add(new Vector2X(finalX, finalY)); } //Add last point jitter.Add(end); return(jitter); }
public void Line_SegmentIntersectionHighSymmetry() { var result = LineAlgorithms.LineSegmentIntersection( Vector2.UnitX, -Vector2.UnitX, Vector2.UnitY, -Vector2.UnitY); result.Should().NotBeNull(); result.U1.Should().BeApproximately(0.5f, 0.0001f); result.U2.Should().BeApproximately(0.5f, 0.0001f); Vector2X.Equals(Vector2.Zero, result.IntersectionPoint).Should().BeTrue("Lines should intersect at origin"); }
private Polygon ParsePolygon(string value) { var pointStrings = value.Split(' '); Polygon result = new Polygon(); foreach (var pt in pointStrings) { result.Add(Vector2X.Parse(pt)); } return(result); }
//Point inside polygon (convex or concave) public static bool PointInPolygon(List <Vector2X> poly, Vector2X point) { bool c = false; int i, j; for (i = 0, j = poly.Count - 1; i < poly.Count; j = i++) { if (((poly[i].value.y > point.value.y) != (poly[j].value.y > point.value.y)) && (point.value.x < (poly[j].value.x - poly[i].value.x) * (point.value.y - poly[i].value.y) / (poly[j].value.y - poly[i].value.y) + poly[i].value.x)) { c = !c; } } return(c); }
private static Polygon RegularShape(float angle, int pointCount, float size) { float step = 2 * (float)Math.PI / pointCount; Polygon result = new Polygon(); for (int i = 0; i < pointCount; i++) { result.Add(Vector2X.FromPolar(size, angle)); angle += step; } return(result); }
public void CD_ContactPoint() { var pa = Diamond; var pb = new RectangleF(0.9f, -1, 1, 2).ToPolygon(); var contactPoint = collider.FindConvexContactPoint(pa, pb); contactPoint.FirstPolygon.Should().BeEquivalentTo(pa); contactPoint.SecondPolygon.Should().BeEquivalentTo(pb); Vector2X.Equals(new Vector2(0.1f, 0), contactPoint.PenetrationDepth, 1e-6f).Should().BeTrue( $"Penetration depth failed. Expected (0.1, 0) but got {contactPoint.PenetrationDepth}."); contactPoint.FirstPolygonContactPoint.Should().Be(new Vector2(1, 0)); contactPoint.SecondPolygonContactPoint.Should().Be(new Vector2(-0.5f, 0)); }
public void V2_ProjectionContract() { ProjectionTest(new Vector2(1, 0), new Vector2(1, 1), new Vector2(0.5f, 0.5f)); for (int i = 0; i < 360; i++) { const float radius = 2; Vector2 v = Vector2X.FromPolarDegrees(radius, i); Vector2 direction = new Vector2(1, 1); direction.Normalize(); Vector2 expected = radius * MathF.Cos((i - 45) * MathF.PI / 180) * direction; ProjectionTest(v, direction, expected); } }
public virtual DisplayObjectX hitTest(Vector2X localVector, bool forTouch = false) { if (!forTouch && (!mVisible || !mTouchable)) { return(null); } // otherwise, check bounding box if (getBounds(this).Contains(localVector)) { return(this); } else { return(null); } }
protected void init(float w, float h) { vertices = new Vector3X[4]; translatorVertices = new Vector3X[4]; uvs = new Vector2X[4]; vertices[0] = new Vector3X(0, 0); vertices[1] = new Vector3X(w, 0); vertices[2] = new Vector3X(w, h); vertices[3] = new Vector3X(0, h); uvs[0] = new Vector2X(0, 0); uvs[1] = new Vector2X(1, 0); uvs[2] = new Vector2X(1, -1); uvs[3] = new Vector2X(0, -1); }
public ConstraintDerivative Derivative(PhysicalParticle particle, Tuple <PhysicalParticle, PhysicalParticle> pair) { var contactPoint = collider.FindConvexContactPoint(pair.Item1.TransformedPolygon, pair.Item2.TransformedPolygon); if (contactPoint.Contact == false) { return(new ConstraintDerivative()); } var firstParticle = particle == pair.Item1; var sign = firstParticle ? -1 : 1; var force = sign * contactPoint.FirstPolygonNormal; var r = firstParticle ? contactPoint.FirstPolygonContactPoint : contactPoint.SecondPolygonContactPoint; var torque = -Vector2X.Cross(r, force); // minus sign because r points from the center to the contact point instead of the other way. return(new ConstraintDerivative(force.X, force.Y, torque)); }
public void CD_ContactPointGradual() { for (float d = 0.1f; d < 1; d += 0.05f) { var pa = new Polygon(Diamond.Points.Select(x => 10 * x)); var pb = new RectangleF(10 - d, -10, 10, 20).ToPolygon(); var contactPoint = collider.FindConvexContactPoint(pa, pb); contactPoint.FirstPolygon.Should().BeEquivalentTo(pa); contactPoint.SecondPolygon.Should().BeEquivalentTo(pb); Vector2X.Equals(new Vector2(d, 0), contactPoint.PenetrationDepth, 1e-6f).Should().BeTrue($"Penetration depth test failed at {d}."); Vector2X.Equals(new Vector2(10, 0), contactPoint.FirstPolygonContactPoint, 1e-6f).Should().BeTrue( $"Contact point on diamond failed at {d}. Expected (10, 0) but got {contactPoint.FirstPolygonContactPoint}"); Vector2X.Equals(new Vector2(-5, 0), contactPoint.SecondPolygonContactPoint, 1e-6f).Should().BeTrue( $"Contact point on rectangle failed at {d}. Expected (-5, 0) but got {contactPoint.SecondPolygonContactPoint}"); } }
public void CD_GJK_EPA_GradualPenetrationDepth() { for (float d = 0.05f; d < 1; d += 0.05f) { var pa = new Polygon(Diamond.Points.Select(x => 10 * x)); var pb = new RectangleF(10 - d, -10, 10, 20).ToPolygon(); var gjk = new GilbertJohnsonKeerthiAlgorithm(); var simplex = gjk.FindMinkowskiSimplex(pa, pb); var epa = new ExpandingPolytopeAlgorithm(); var pv = epa.PenetrationDepth( v => GilbertJohnsonKeerthiAlgorithm.PolygonSupport(pa, v), v => GilbertJohnsonKeerthiAlgorithm.PolygonSupport(pb, v), simplex); simplex.ContainsOrigin.Should().BeTrue($"Collision not detected by Minkowski simplex at {d}."); Vector2X.Equals(new Vector2(d, 0), pv.Value, 1e-6f).Should().BeTrue($"Penetration depth test failed at {d}."); } }
/** * 取得一个索引项的位置; * @param i * */ protected Vector2X getPosition(int i, bool hasBorder = true) { Vector2X point = new Vector2X(); if (_vertical) { if (_maxDirect == -1) { point.x = 0; point.y = _itemBoundHeight * i; } else { point.x = (i / _maxDirect) * _itemBoundWidth; point.y = (i % _maxDirect) * _itemBoundHeight; } } else { if (_maxDirect == -1) { point.x = _itemBoundWidth * i; point.y = 0; } else { point.y = (i / _maxDirect) * _itemBoundHeight; point.x = (i % _maxDirect) * _itemBoundWidth; } } if (hasBorder) { point.x += _border.left; point.y += _border.top; } return(point); }
private void touchHandle(EventX e) { Vector2X p = (Vector2X)e.data; float stageX = p.x; float stageY = p.y; switch (e.type) { case MouseEventX.MOUSE_DOWN: mx = mx0 = stageX; my = my0 = stageY; _stage = stage; _stage.addEventListener(MouseEventX.MOUSE_MOVE, touchMoveHandle); _stage.addEventListener(MouseEventX.MOUSE_UP, touchHandle); break; case MouseEventX.MOUSE_UP: _stage.removeEventListener(MouseEventX.MOUSE_MOVE, touchMoveHandle); _stage.removeEventListener(MouseEventX.MOUSE_UP, touchHandle); if (_isDragging) { touchEnd(stageX - mx, stageY - my, stageX - mx0, stageY - my0); if (hasEventListener(TouchEventX.TOUCH_END)) { this.dispatchEvent(new TouchEventX(TouchEventX.TOUCH_END, stageX - mx, stageY - my, stageX - mx0, stageY - my0)); } _isDragging = false; CallLater.Add(touchEndHandle); } break; } }
private void touchMoveHandle(EventX e) { Vector2X p = (Vector2X)e.data; float stageX = p.x; float stageY = p.y; float dx = stageX - mx0; float dy = stageY - my0; if (!_isDragging) { if (Math.Abs(dx) < draggingDistanceX || Math.Abs(dy) < draggingDistanceY || (dx * dx + dy * dy) < MIN_DISTANCE_SQ) { return; } _isDragging = true; _draggingTarget = this; CallLater.Remove(touchEndHandle); touchStart(dx, dy);//这里面可能会有需求再变为不可拖; if (hasEventListener(TouchEventX.TOUCH_START)) { this.dispatchEvent(new TouchEventX(TouchEventX.TOUCH_START, 0, 0, dx, dy)); } } touchMoving(stageX - mx, stageY - my, dx, dy); if (hasEventListener(TouchEventX.TOUCH_START)) { this.dispatchEvent(new TouchEventX(TouchEventX.TOUCH_START, stageX - mx, stageY - my, dx, dy)); } mx = stageX; my = stageY; }
public Vector2Edge(Vector2X Start, Vector2X End) { start = Start; end = End; }