public static Side WhereIAm(this Point point, Rectangle rect) { if (rect.Size.Width == 0 || rect.Size.Height == 0) { return(Side.Nowhere); } var rect_centr = rect.Location.Add(((Point)rect.Size).Multiply(0.5f)); var to_leftUp = rect.Location.Sub(rect_centr); var to_leftDown = new Point(to_leftUp.X, to_leftUp.Y + rect.Size.Height); var normalize_to_leftUp = to_leftUp.Normalize(); var normalize_to_leftDown = to_leftDown.Normalize(); var normalize_to_point = point.Sub(rect_centr).Normalize(); if (normalize_to_point.Y < normalize_to_leftUp.Y) { return(Side.Up); } else if (normalize_to_point.Y > normalize_to_leftDown.Y) { return(Side.Down); } else if (normalize_to_point.X > 0) { return(Side.Right); } else if (normalize_to_point.X < 0) { return(Side.Left); } else { return(Side.Nowhere); } }
public void TestNormalizeEmptyPoint() { Point point = (Point)reader.Read("POINT EMPTY"); point.Normalize(); Assert.AreEqual(null, point.Coordinate); }
/// <summary> /// /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <param name="third"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="padding"></param> /// <returns>number of new points</returns> static int GetPaddedCorner(PolylinePoint first, PolylinePoint second, PolylinePoint third, out Point a, out Point b, double padding) { Point u = first.Point; Point v = second.Point; Point w = third.Point; System.Diagnostics.Debug.Assert(Point.GetTriangleOrientation(u, v, w) == TriangleOrientation.Clockwise); Point uvPerp = (v - u).Rotate(Math.PI / 2).Normalize(); ////uvPerp has to look outside of the curve //if (uvPerp * (v - Origin) < 0) // uvPerp *= -1; Point l = (v - u).Normalize() + (v - w).Normalize(); if (l.Length < ApproximateComparer.IntersectionEpsilon) { a = b = v + padding * uvPerp; return(1); } Point d = l.Normalize() * padding; Point dp = d.Rotate(Math.PI / 2); //look for a in the form d+x*dp //we have: Padding=(d+x*dp)*uvPerp double xp = (padding - d * uvPerp) / (dp * uvPerp); a = d + xp * dp + v; b = d - xp * dp + v; return(2); //number of points to add }
/// <summary> /// this code should never work! /// </summary> /// <param name="edge"></param> /// <param name="a"></param> /// <param name="b"></param> static void CreateEdgeCurveWithNoTrimming(Edge edge, Point a, Point b) { Point ab = b - a; ab = ab.Normalize(); Point lineStart = a; Point lineEnd = b; Arrowhead targetArrow = edge.EdgeGeometry.TargetArrowhead; if (targetArrow != null) { targetArrow.TipPosition = b; lineEnd = b - ab * targetArrow.Length; } Arrowhead sourceArrow = edge.EdgeGeometry.SourceArrowhead; if (sourceArrow != null) { sourceArrow.TipPosition = a; lineStart = a + ab * sourceArrow.Length; } edge.Curve = new LineSegment(lineStart, lineEnd); }
public override void Update() { base.Update(); if (!OnCamera) { var offX = Left > FP.Width || Right < 0; var offY = Top > FP.Height || Bottom < 0; if (offX && offY) { World.Remove(this); } } var l = new List <Entity>(); CollideInto(Player.Collision, X, Y, l); foreach (var p in l) { var player = p as Player; if (player != owner) { if (player.Rebounding) { World.BroadcastMessage(CameraManager.Message.Shake, 10.0f, 0.5f); Mixer.Hit1.Play(); var angle = FP.Angle(player.X, player.Y, owner.X, owner.Y); FP.AngleXY(ref direction.X, ref direction.Y, angle, 1); owner = player; } } if (player != owner) { if (!player.Invincible) { World.BroadcastMessage(CameraManager.Message.Shake, 10.0f, 0.5f); Mixer.Hit1.Play(); var force = ForceMultiplier * Fist.BASE_PUNCH_FORCE; var dir = direction.Normalized(); player.OnMessage(PhysicsBody.Message.Impulse, force * dir.X, force * dir.Y); } } } image.Angle = FP.Angle(0, 0, direction.X, direction.Y); direction.Normalize(FistSpeed); var rainbow = World.Add(new RainbowTrail(X, Y, direction, image.Scale, FistSpeed, Layer)); X += direction.X; Y += direction.Y; }
/// <summary> /// 求两个向量的夹角余弦值. /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <returns></returns> public static double IncludedAngle(this Point p1, Point p2) { p1.Normalize(); p2.Normalize(); double dot = Dot(p1, p2); return(2 * dot / (p1.Magnitude() + p2.Magnitude())); }
public void TestNormalizePoint() { Point point = (Point)reader.Read("POINT (30 30)"); point.Normalize(); Assert.AreEqual(new Coordinate(30, 30), point.Coordinate); }
public GhostMoveAction(Player player, Point target) { m_player = player; m_target = target; m_isFinished = false; m_v = new Point(target.X - m_player.X, target.Y - m_player.Y); m_v.Normalize(2); }
public GhostMoveAction(Player player, Point target) : base(0, 1000) { m_player = player; m_target = target; m_v = new Point(target.X - m_player.X, target.Y - m_player.Y); m_v.Normalize(2); }
public void TestNormalize() { Point p1 = new Point(3, 5); Point p2 = p1.Normalize(100, 100); Assert.AreEqual((double)p1.X / 100, p2.X); Assert.AreEqual((double)p1.Y / 100, p2.Y); }
public GhostMoveAction(Player player,Point target) { m_player = player; m_target = target; m_isFinished = false; m_v = new Point(target.X - m_player.X, target.Y - m_player.Y); m_v.Normalize(2); }
/// <summary> /// closest point on the segment /// </summary> /// <param name="p1">first endpoint</param> /// <param name="p2">second endpoint</param> /// <param name="x">reference point</param> /// <returns></returns> public static Point ClosestPointOnSegment(Point p1, Point p2, Point x) { Point l = (p2 - p1); Point v = l.Normalize(); double dot = Point.Dot(v, x - p1) / l.Length; Point p = p1 + l * dot; return(dot <= 0 ? p1 : (dot >= 1 ? p2 : p1 + l * dot)); }
public void DotProduct() { var point1 = new Point(1, 1); point1.Normalize(); var point2 = new Point(-1, 1); point2.Normalize(); Assert.AreEqual(0.0f, point1.DotProduct(point2)); Assert.AreEqual(0.7071f, point1.DotProduct(Point.UnitY), 0.0001f); }
private static Point Projection(Point vectorA, Point vectorB) { Point vectorBUnit = new Point(vectorB.X, vectorB.Y); vectorBUnit = vectorBUnit.Normalize(); decimal dotProduct = vectorA.X * vectorBUnit.X + vectorA.Y * vectorBUnit.Y; return(vectorBUnit.MultiplyByDecimal(dotProduct)); }
internal Parallelogram(Parallelogram box0, Parallelogram box1) { Point v = box0.Corner; double minX, maxX, minY, maxY; minX = maxX = v.X; minY = maxY = v.Y; PumpMinMax(ref minX, ref maxX, ref minY, ref maxY, ref box0.aPlusCorner); PumpMinMax(ref minX, ref maxX, ref minY, ref maxY, ref box0.otherCorner); PumpMinMax(ref minX, ref maxX, ref minY, ref maxY, ref box0.bPlusCorner); PumpMinMax(ref minX, ref maxX, ref minY, ref maxY, ref box1.corner); PumpMinMax(ref minX, ref maxX, ref minY, ref maxY, ref box1.aPlusCorner); PumpMinMax(ref minX, ref maxX, ref minY, ref maxY, ref box1.otherCorner); PumpMinMax(ref minX, ref maxX, ref minY, ref maxY, ref box1.bPlusCorner); this.corner = new Point(minX, minY); this.a = new Point(0, maxY - minY); this.b = new Point(maxX - minX, 0); aPlusCorner = a + corner; otherCorner = b + aPlusCorner; bPlusCorner = b + corner; this.aRot = new Point(-this.a.Y, this.a.X); if (aRot.Length > 0.5) { aRot = aRot.Normalize(); } this.bRot = new Point(-this.b.Y, this.b.X); if (bRot.Length > 0.5) { bRot = bRot.Normalize(); } abRot = this.a * bRot; baRot = this.b * aRot; if (abRot < 0) { abRot = -abRot; bRot = -bRot; } if (baRot < 0) { baRot = -baRot; aRot = -aRot; } isSeg = (this.a - this.b).Length < ApproximateComparer.DistanceEpsilon; }
public void NormalizeVector_vector_010_result_01() { var vector = new Point(0, 10); var expect = new Point(0, 1); var temp_result = vector.Normalize(); var result = temp_result.ToPoint(); Assert.AreEqual(expect, result); }
/// <summary> /// /// </summary> public virtual double Project() { Point uv = v.Center - u.Center; double d = separation - uv.Length, wu = ((FiNode)u.AlgorithmData).stayWeight, wv = ((FiNode)v.AlgorithmData).stayWeight; Point f = d * uv.Normalize() / (wu + wv); u.Center -= wv * f; v.Center += wu * f; return(Math.Abs(d)); }
private bool VelocityChanged(Point PuckVold, Point PuckVnew) { Point oldVelocity = PuckVold.Normalize(); Point newVelocity = PuckVnew.Normalize(); if ((PuckVnew.Norm() <= PuckVold.Norm()) && (PuckVnew.Norm() >= 0.95 * PuckVold.Norm()) && (Math.Abs(PuckVnew.Angle() - PuckVold.Angle()) < 0.06)) { return(false); } return(true); }
private void Joystick_Move(object sender, TouchEventArgs e) { #if NOESIS var joystickPosition = e.GetTouchPoint(_joystick) - JoystickCenter; joystickPosition /= JoystickRadius; if (Point.Length(joystickPosition) > 1f) { joystickPosition = Point.Normalize(joystickPosition); } UpdateKnobPosition(joystickPosition); e.Handled = true; #endif }
double GetLeftTipParam(double portParam, Point portPoint, Curve paddingCurve, Node node, double length) { bool curveIsClockwise = InteractiveObstacleCalculator.CurveIsClockwise(node.BoundaryCurve, node.Center); Point tan = curveIsClockwise ? node.BoundaryCurve.LeftDerivative(portParam) : -node.BoundaryCurve.RightDerivative(portParam); tan = ((-tan.Normalize()) * length).Rotate(-EnteringAngle); IList <IntersectionInfo> xs = Curve.GetAllIntersections(paddingCurve, new LineSegment(portPoint, portPoint + tan), true); Debug.Assert(xs.Count == 1); return(xs[0].Par0); }
public static void DrawArrowCap(this Graphics g, Pen pen, Point destination, Point guideVector, int length, int width, bool closedLine = false) { var normal_guide_vector = guideVector.Normalize(); var start_arrow_point = destination.Sub(normal_guide_vector.Multiply(length).ToPoint()); var first_arrow_point = start_arrow_point.Add(normal_guide_vector.Perpendicular().Multiply(width / 2).ToPoint()); var second_arrow_point = start_arrow_point.Add(normal_guide_vector.Perpendicular().Multiply(-width / 2).ToPoint()); g.DrawLine(pen, first_arrow_point, destination); g.DrawLine(pen, second_arrow_point, destination); if (closedLine) { g.DrawLine(pen, first_arrow_point, second_arrow_point); } }
public void StartGhostMoving() { if (this.TargetPoint.IsEmpty) { return; } Point point = new Point(this.TargetPoint.X - this.X, this.TargetPoint.Y - this.Y); if (point.Length() > 160.0) { point.Normalize(160); } this.m_game.AddAction(new GhostMoveAction(this, new Point(this.X + point.X, this.Y + point.Y))); }
/// <summary> /// /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <param name="third"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="padding"></param> /// <returns>number of new points</returns> static int GetPaddedCorner(PolylinePoint first, PolylinePoint second, PolylinePoint third, out Point a, out Point b, double padding) { Point u = first.Point; Point v = second.Point; Point w = third.Point; if (Point.GetTriangleOrientation(u, v, w) == TriangleOrientation.Counterclockwise) { a = new Point(); b = new Point(); return(-1); } Point uvPerp = (v - u).Rotate(Math.PI / 2).Normalize(); if (CornerIsNotTooSharp(u, v, w)) { //the angle is not too sharp: just continue the offset lines of the sides and return their intersection uvPerp *= padding; Point vwPerp = ((w - v).Normalize() * padding).Rotate(Math.PI / 2); bool result = Point.LineLineIntersection(u + uvPerp, v + uvPerp, v + vwPerp, w + vwPerp, out a); Debug.Assert(result); b = a; return(1); } Point l = (v - u).Normalize() + (v - w).Normalize(); if (l.Length < ApproximateComparer.IntersectionEpsilon) { a = b = v + padding * uvPerp; return(1); } Point d = l.Normalize() * padding; Point dp = d.Rotate(Math.PI / 2); //look for a in the form d+x*dp //we have: Padding=(d+x*dp)*uvPerp double xp = (padding - d * uvPerp) / (dp * uvPerp); a = d + xp * dp + v; b = d - xp * dp + v; return(2); //number of points to add }
public override EffectMessage MakeEffect() { EffectMessage.Callback callback = delegate(Entity from, Entity to, float scalar) { if (to == from) { return; // sender; } var dir = new Point(to.X - from.X, to.Y - from.Y); dir.Normalize(MagnetStrength); to.OnMessage(PhysicsBody.Message.Impulse, -dir.X, -dir.Y); }; return(new EffectMessage(owner, callback)); }
public void StartGhostMoving() { if (!this.TargetPoint.IsEmpty) { Point pv = new Point(this.TargetPoint.X - this.X, this.TargetPoint.Y - this.Y); if (pv.Length() != 0.0) { Point target = this.TargetPoint; if (pv.Length() > (double)this.m_ghostEnergy) { pv = pv.Normalize(this.m_ghostEnergy); } Point p = new Point(this.X + pv.X, this.Y + pv.Y); this.m_game.AddAction(new GhostMoveAction(this, p)); } } }
public void StartGhostMoving() { if (TargetPoint.IsEmpty) { return; } Point pv = new Point(TargetPoint.X - X, TargetPoint.Y - Y); Point target = TargetPoint; if (pv.Length() > 160) { pv.Normalize(160); } m_game.AddAction(new GhostMoveAction(this, new Point(X + pv.X, Y + pv.Y))); }
Point GetBestRedlinePoint(IEnumerable <Point> redlinePoints, Point origin, Point direction) { var potentialPoints = redlinePoints.Where(p => p.DistanceSq(origin) < 180 * 180).ToArray(); if (potentialPoints.Length == 0) { // no potential points?? redlinePoints.OrderBy(p => p.DistanceSq(origin)).First(); } var refDir = direction.Normalize(); return(potentialPoints.OrderByDescending(p => { var dir = p.Subtract(origin).Normalize(); var scalar = refDir.Item1 * dir.Item1 + refDir.Item2 * dir.Item2; return scalar; }).First()); }
/// <summary> /// Constructs the parallelogram by the corner and two sides. /// </summary> /// <param name="corner">the corner</param> /// <param name="sideA">a side</param> /// <param name="sideB">another side</param> public Parallelogram(Point corner, Point sideA, Point sideB) { this.corner = corner; this.a = sideA; this.b = sideB; this.aRot = new Point(-sideA.Y, sideA.X); if (aRot.Length > 0.5) { aRot = aRot.Normalize(); } this.bRot = new Point(-sideB.Y, sideB.X); if (bRot.Length > 0.5) { bRot = bRot.Normalize(); } abRot = sideA * bRot; baRot = sideB * aRot; if (abRot < 0) { abRot = -abRot; bRot = -bRot; } if (baRot < 0) { baRot = -baRot; aRot = -aRot; } isSeg = (sideA - sideB).Length < ApproximateComparer.DistanceEpsilon; aPlusCorner = sideA + corner; otherCorner = sideB + aPlusCorner; bPlusCorner = sideB + corner; }
public void calculateNormalsAndDs() { triangleNormals.Clear(); trianglePlaneEquationDs.Clear(); foreach (TriangleNet.Data.Triangle t in triangles) { Point p0 = points[t.P0]; Point p1 = points[t.P1]; Point p2 = points[t.P2]; Point v1 = p1 - p0; Point v2 = p2 - p0; Point normal = Point.CrossProduct(ref v1, ref v2); normal.Normalize(); triangleNormals.Add(normal); trianglePlaneEquationDs.Add(-normal.X * points[t.P0].X - normal.Y * points[t.P0].Y - normal.Z * points[t.P0].Z); } }
private Point GetNaiveCollisionPoint(AHEntities.Action direction, Point puckPline, double puckRadius, int tableW, int tableH) { Point attackVector = new Point(); Point collisionPoint = null; Point wallCollision = null; double ratio = 0; switch (direction) { case AHEntities.Action.ATTACK_RIGHT: ratio = Math.Abs(puckPline.Y - (-tableH / 2)) / (tableH / 2); wallCollision = new Point(Math.Abs(tableW / 2 - puckPline.X) / (1 + ratio) + puckPline.X, -tableH / 2); attackVector = (wallCollision - puckPline).Normalize(); attackVector.Set(attackVector.X * puckRadius, attackVector.Y * puckRadius); collisionPoint = puckPline - attackVector; break; case AHEntities.Action.ATTACK_MIDDLE: attackVector.X = tableW / 2 - puckPline.X; attackVector.Y = -puckPline.Y; attackVector = attackVector.Normalize(); attackVector.Set(attackVector.X * puckRadius, attackVector.Y * puckRadius); collisionPoint = puckPline - attackVector; break; case AHEntities.Action.ATTACK_LEFT: ratio = Math.Abs(puckPline.Y - tableH / 2) / (tableH / 2); wallCollision = new Point(Math.Abs(tableW / 2 - puckPline.X) / (1 + ratio) + puckPline.X, tableH / 2); attackVector = (wallCollision - puckPline).Normalize(); attackVector.Set(attackVector.X * puckRadius, attackVector.Y * puckRadius); collisionPoint = puckPline - attackVector; break; default: collisionPoint = null; break; } return(collisionPoint); }
public static Matrix <double> FindOrthoNormalMatrix(Point point1, Point point2, Point point3) { Point v1 = Point.Normalize(point2 - point1); Point v2 = Point.Normalize(point2 - point3); Point normal = Point.Normalize(Point.cross(v1, v2)); Point v2_ortho = Point.Normalize(Point.cross(normal, v1)); Matrix <double> T = Matrix <double> .Build.Dense(3, 3); T[0, 0] = v1.x; T[1, 0] = v1.y; T[2, 0] = v1.z; T[0, 1] = v2_ortho.x; T[1, 1] = v2_ortho.y; T[2, 1] = v2_ortho.z; T[0, 2] = normal.x; T[1, 2] = normal.y; T[2, 2] = normal.z; return(T); }
internal static ICurve CreateBaseSegmentForDraggingEdgeWithSource(Point delta, GeomEdge edge, EdgeRestoreData edgeRestoreData, Dictionary <GeomEdge, double> offsets) { double offset; ICurve seg; Point a = edgeRestoreData.UnderlyingPolyline.HeadSite.Point + delta; Point b = edgeRestoreData.UnderlyingPolyline.LastSite.Point; if (offsets.TryGetValue(edge, out offset) && offset != 0) { Point ab = b - a; Point abOrtog = ab.Normalize().Rotate(Math.PI / 2) * offset; seg = CreateBaseSegOnSourceTargetAndOrth(ref a, ref b, ref abOrtog); // SugiyamaLayoutSettings.Show(seg, edge.Curve, edge.Source.BoundaryCurve); } else { seg = new LineSegment(a, b); } return(seg); }
protected override void OnRefresh() { RotarySwitch rotarySwitch = Visual as RotarySwitch; if (rotarySwitch != null) { _imageRect.Width = rotarySwitch.Width; _imageRect.Height = rotarySwitch.Height; _image = ConfigManager.ImageManager.LoadImage(rotarySwitch.KnobImage); _imageBrush = new ImageBrush(_image); _center = new Point(rotarySwitch.Width / 2d, rotarySwitch.Height / 2d); _lines = new GeometryDrawing(); _lines.Pen = new Pen(new SolidColorBrush(rotarySwitch.LineColor), rotarySwitch.LineThickness); _labels.Clear(); Vector v1 = new Point(_center.X, 0) - _center; double lineLength = v1.Length * rotarySwitch.LineLength; double labelDistance = v1.Length * rotarySwitch.LabelDistance; v1.Normalize(); GeometryGroup lineGroup = new GeometryGroup(); Brush labelBrush = new SolidColorBrush(rotarySwitch.LabelColor); foreach (RotarySwitchPosition position in rotarySwitch.Positions) { Matrix m1 = new Matrix(); m1.Rotate(position.Rotation); if (rotarySwitch.DrawLines) { Vector v2 = v1 * m1; Point startPoint = _center; Point endPoint = startPoint + (v2 * lineLength); lineGroup.Children.Add(new LineGeometry(startPoint, endPoint)); } if (rotarySwitch.DrawLabels) { FormattedText labelText = rotarySwitch.LabelFormat.GetFormattedText(labelBrush, position.Name); labelText.TextAlignment = TextAlignment.Center; labelText.MaxTextWidth = rotarySwitch.Width; labelText.MaxTextHeight = rotarySwitch.Height; if (rotarySwitch.MaxLabelHeight > 0d && rotarySwitch.MaxLabelHeight < rotarySwitch.Height) { labelText.MaxTextHeight = rotarySwitch.MaxLabelHeight; } if (rotarySwitch.MaxLabelWidth > 0d && rotarySwitch.MaxLabelWidth < rotarySwitch.Width) { labelText.MaxTextWidth = rotarySwitch.MaxLabelWidth; } Point location = _center + (v1 * m1 * labelDistance); if (position.Rotation <= 10d || position.Rotation >= 350d) { location.X -= labelText.MaxTextWidth / 2d; location.Y -= labelText.Height; } else if (position.Rotation < 170d) { location.X -= (labelText.MaxTextWidth - labelText.Width) / 2d; location.Y -= labelText.Height / 2d; } else if (position.Rotation <= 190d) { location.X -= labelText.MaxTextWidth / 2d; } else { location.X -= (labelText.MaxTextWidth + labelText.Width) / 2d; location.Y -= labelText.Height / 2d; } _labels.Add(new SwitchPositionLabel(labelText, location)); } } _lines.Geometry = lineGroup; _lines.Freeze(); } else { _image = null; _imageBrush = null; _lines = null; _labels.Clear(); } }
public void StartGhostMoving() { if (TargetPoint.IsEmpty) return; Point pv = new Point(TargetPoint.X - X, TargetPoint.Y - Y); Point target = TargetPoint; if (pv.Length() > 160) { pv.Normalize(160); } m_game.AddAction(new GhostMoveAction(this, new Point(X + pv.X, Y + pv.Y))); }
public override void StartMoving() { m_actions = new List<BombAction>(); while (m_isLiving) { m_lifeTime += 0.04F; Point pos = CompleteNextMovePoint(0.04F); MoveTo(pos.X,pos.Y); if (m_isLiving) { if (Math.Round(m_lifeTime * 100) % 40 == 0 && pos.Y > 0) m_game.AddTempPoint(pos.X, pos.Y); if (m_controled && vY > 0) { Player player = m_map.FindNearestEnemy(m_x, m_y, 300, m_owner); if (player != null) { Point v = new Point(player.X - m_x, player.Y - m_y); v.Normalize(1000); setSpeedXY(v.X, v.Y); m_actions.Add(new BombAction(m_lifeTime, ActionType.CHANGE_SPEED, v.X, v.Y, 0,0)); //使炮弹不受重力和风力和空气阻力的影响 UpdateForceFactor(0, 0, 0); m_controled = false; } } } } }
public void Normalize() { var point = new Point(0.3f, -0.4f); point.Normalize(); Assert.AreEqual(new Point(0.6f, -0.8f), point); }