// ----------------------- // Public Constructors // ----------------------- /// <summary> /// Creates a RectangleF from PointF and SizeF values. /// </summary> /// <param name="location">Location.</param> /// <param name="size">Size.</param> public RectangleF(PointF location, SizeF size) { x = location.X; y = location.Y; width = size.Width; height = size.Height; }
/// <summary> /// Fits the covariance matrix (or 2nd moment matrix) to the ellipse by calculating eigen-vectors and values. /// </summary> /// <param name="covMatrix">Covariance matrix (or 2nd moment matrix).</param> /// <param name="center">Center of the ellipse.</param> /// <returns>Ellipse.</returns> public static Ellipse Fit(double[,] covMatrix, PointF center = default(PointF)) { if (covMatrix.GetLength(0) != 2 || covMatrix.GetLength(1) != 2) throw new ArgumentException("Covariance matrix must have the same dimensions, and the dimension length must be 2!"); return Fit(covMatrix[0, 0], covMatrix[0, 1], covMatrix[1, 1], center); }
public void Diffuse() { this.Position = new PointF { X = this.Position.X + 25 * (float)normalDistribution.Generate(), Y = this.Position.Y + 25 * (float)normalDistribution.Generate(), }; }
public override void OnMouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left || !this.IsSelected || isDrawn) return; pt = Element.ToImageCoordinate(e.Location).ToPt(); var imageSize = Element.Image.Size.ToSize(); this.Annotation.Polygon = new PointF[] { pt.Clamp(imageSize) }; isDrawn = true; }
/// <summary> /// Produces a Point structure from a PointF structure by /// taking the ceiling of the X and Y properties. /// </summary> /// <param name="value">Floating-point coordinate pair.</param> /// <returns>Integer coordinate pair.</returns> public static Point Ceiling(PointF value) { int x, y; checked { x = (int)Math.Ceiling(value.X); y = (int)Math.Ceiling(value.Y); } return new Point(x, y); }
private void initializeKalman(PointF startPoint) { var measurementDimension = 2; //just coordinates var initialState = new ModelState { Position = startPoint, Velocity = new PointF(0.2f, -1.5f)}; var initialStateError = ModelState.GetProcessNoise(10); kalman = new DiscreteKalmanFilter<ModelState, PointF>(initialState, initialStateError, measurementDimension /*(position)*/, 0 /*no control*/, x => ModelState.ToArray(x), x => ModelState.FromArray(x), x => new double[] { x.X, x.Y }); kalman.ProcessNoise = ModelState.GetProcessNoise(1); kalman.MeasurementNoise = Matrix.Diagonal<double>(kalman.MeasurementVectorDimension, 10000.0); kalman.MeasurementMatrix = ModelState.GetPositionMeasurementMatrix(); kalman.TransitionMatrix = ModelState.GetTransitionMatrix(); }
/// <summary> /// Creates new structure from area and angle. /// </summary> /// <param name="center">Box2D center.</param> /// <param name="size">Box 2D size.</param> /// <param name="angle">Angle in degrees.</param> public Box2D(PointF center, SizeF size, float angle) { this.Center = new PointF(center.X, center.Y); this.Size = size; this.Angle = angle; }
/// <summary> /// Creates new <see cref="Vector2D"/> structure. /// </summary> /// <param name="startPoint">Start point.</param> /// <param name="endPoint">End point.</param> public Vector2D(PointF startPoint, PointF endPoint) : this(endPoint.X - startPoint.X, endPoint.Y - startPoint.Y) { }
/// <summary> /// Translates a PointF by the positive of a specified size. /// </summary> /// <param name="pt">Point.</param> /// <param name="sz">Offset.</param> /// <returns>PointF structure.</returns> public static PointF Add(PointF pt, Size sz) { return(new PointF(pt.X + sz.Width, pt.Y + sz.Height)); }
/// <summary> /// Translates a PointF by the negative of a specified size. /// </summary> /// <param name="pt">Point.</param> /// <param name="sz">Offset.</param> /// <returns>PointF structure.</returns> public static PointF Subtract(PointF pt, Size sz) { return new PointF(pt.X - sz.Width, pt.Y - sz.Height); }
/// <summary> /// Constructs an empty model. /// </summary> public ConstantAcceleration2DModel() { this.Position = default(PointF); this.Velocity = default(PointF); this.Acceleration = default(PointF); }
/// <summary> /// Produces a Point structure from a PointF structure by /// rounding the X and Y properties. /// </summary> /// <param name="value">Floating-point coordinate pair.</param> /// <returns>Integer coordinate pair.</returns> public static Point Round(PointF value) { int x, y; checked { x = (int)Math.Round(value.X); y = (int)Math.Round(value.Y); } return new Point(x, y); }
//taken from:http://stackoverflow.com/questions/4243042/c-sharp-point-in-polygon and modified /// <summary> /// Checks whether the specified location is in the polygon. /// </summary> /// <param name="poly">Polygon.</param> /// <param name="x">Horizontal coordinate.</param> /// <param name="y">VErtical coordinate.</param> /// <returns>True if the point resides inside the polygon, false otherwise.</returns> public static bool IsInPolygon(this IList<PointF> poly, float x, float y) { PointF p1, p2; bool inside = false; if (poly.Count < 3) { return inside; } var oldPoint = new PointF(poly[poly.Count - 1].X, poly[poly.Count - 1].Y); for (int i = 0; i < poly.Count; i++) { var newPoint = new PointF(poly[i].X, poly[i].Y); if (newPoint.X > oldPoint.X) { p1 = oldPoint; p2 = newPoint; } else { p1 = newPoint; p2 = oldPoint; } if ((newPoint.X < x) == (x <= oldPoint.X) && (y - (long)p1.Y) * (p2.X - p1.X) < (p2.Y - (long)p1.Y) * (x - p1.X)) { inside = !inside; } oldPoint = newPoint; } return inside; }
/// <summary> /// Calculates the Euclidean distance between two points. /// </summary> /// <param name="pointA">First point.</param> /// <param name="pointB">Second point.</param> /// <returns>Euclidean distance between the points.</returns> public static double DistanceTo(this PointF pointA, PointF pointB) { var distnace = System.Math.Sqrt((pointA.X - pointB.X) * (pointA.X - pointB.X) + (pointA.Y - pointB.Y) * (pointA.Y - pointB.Y)); //Euclidean distance return distnace; }
/// <summary> /// Translates the point by the specified offset. /// </summary> /// <param name="point">The point to offset.</param> /// <param name="offset">Offset to be added.</param> /// <returns>Translated point.</returns> public static PointF Offset(this PointF point, PointF offset) { return new PointF { X = point.X + offset.X, Y = point.Y + offset.Y }; }
private void plotData(PointF point, int seriesIdx, DataPoint dataPointStyle = null, bool markLastPt = true) { Series s = chart.Series[seriesIdx]; dataPointStyle = dataPointStyle ?? new DataPoint(); if (markLastPt) tryRemoveLastMarker(seriesIdx, dataPointStyle); var dataPt = dataPointStyle.Clone(); dataPt.XValue = point.X; dataPt.YValues = new double[] { point.Y }; s.Points.Add(dataPt); if (markLastPt) addMarker(seriesIdx); }
/// <summary> /// Order the points clockwise starting from the 12 o'clock. /// </summary> /// <param name="points">Points to sort clockwise</param> /// <returns>Sorted point indexes.</returns> public static IEnumerable<int> SortPointsClockwise(this IEnumerable<PointF> points) { PointF center = new PointF { X = (float)points.Select(x => x.X).Average(), Y = (float)points.Select(x => x.Y).Average() }; var sortedIndeces = points .Select((x, i) => new { Index = i, Value = (System.Math.Atan2(x.Y - center.Y, x.X - center.X) * 180 / System.Math.PI - 90 + 360) % 360 }) .OrderBy(x => x.Value) .Select(x => x.Index) .ToList(); return sortedIndeces; }
// ----------------------- // Public Constructors // ----------------------- /// <summary> /// SizeF Constructor /// </summary> /// /// <remarks> /// Creates a SizeF from a PointF value. /// </remarks> public SizeF(PointF pt) { width = pt.X; height = pt.Y; }
/// <summary> /// Reflect point over reflector point. /// </summary> private static PointF reflectPoint(PointF pt, PointF reflectorPt) { return new PointF { X = reflectorPt.X + (reflectorPt.X - pt.X), Y = reflectorPt.Y + (reflectorPt.Y - pt.Y) }; }
/// <summary> /// Subtracts the point by the specified offset. /// </summary> /// <param name="point">The point to subtract.</param> /// <param name="offset">Subtract factor.</param> /// <returns>Translated point.</returns> public static PointF Subtract(this PointF point, PointF offset) { return new PointF { X = point.X - offset.X, Y = point.Y - offset.Y }; }
/// <summary> /// Creates a SizeF from a PointF value. /// </summary> /// <param name="pt">PointF.</param> public SizeF(PointF pt) { width = pt.X; height = pt.Y; }
/// <summary> /// Rotates one point around another /// </summary> /// <param name="pointToRotate">The point to rotate.</param> /// <param name="angleDeg">The rotation angle in degrees.</param> /// <param name="centerPoint">The center point of rotation.</param> /// <returns>Rotated point</returns> public static PointF Rotate(this PointF pointToRotate, double angleDeg, PointF centerPoint) { //taken from: http://stackoverflow.com/questions/13695317/rotate-a-point-around-another-point and modified double angleInRadians = angleDeg * (Math.PI / 180); double cosTheta = Math.Cos(angleInRadians); double sinTheta = Math.Sin(angleInRadians); return new PointF { X = (float) (cosTheta * (pointToRotate.X - centerPoint.X) - sinTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.X), Y = (float) (sinTheta * (pointToRotate.X - centerPoint.X) + cosTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.Y) }; }
/// <summary> /// Fits the covariance matrix (or 2nd moment matrix) to the ellipse by calculating eigen-vectors and values. /// </summary> /// <param name="a">[0, 0] value of the covariance matrix.</param> /// <param name="b">[0, 1] or [1, 0] value of the covariance matrix.</param> /// <param name="c">[1, 1] value of the covariance matrix.</param> /// <param name="center">Center of the ellipse.</param> /// <returns>Ellipse.</returns> public static Ellipse Fit(double a, double b, double c, PointF center = default(PointF)) { bool success; return Fit(a, b, c, center, out success); }
/// <summary> /// Gets the center of the mass of the contour. /// </summary> /// <param name="points">Contour points.</param> /// <returns>The center of the mass of the contour.</returns> public static PointF Center(this IEnumerable<Point> points) { PointF average = new PointF(); int nSamples = 0; foreach (var pt in points) { average.X += pt.X; average.Y += pt.Y; nSamples++; } average.X /= nSamples; average.Y /= nSamples; return average; }
/// <summary> /// Fits the covariance matrix (or 2nd moment matrix) to the ellipse by calculating eigen-vectors and values. /// </summary> /// <param name="a">[0, 0] value of the covariance matrix.</param> /// <param name="b">[0, 1] or [1, 0] value of the covariance matrix.</param> /// <param name="c">[1, 1] value of the covariance matrix.</param> /// <param name="center">Center of the ellipse.</param> /// <param name="success">Returns true if both calculated eigen-values are positive.</param> /// <returns>Ellipse.</returns> public static Ellipse Fit(double a, double b, double c, PointF center, out bool success) { var acDiff = a - c; var acSum = a + c; //A * X = lambda * X => solve quadratic equation => lambda1/2 = [a + c +/- sqrt((a-c)^2 + 4b^2)] / 2 var sqrtDiscriminant = System.Math.Sqrt(acDiff * acDiff + 4 * b * b); var eigVal1 = (acSum + sqrtDiscriminant) / 2; var eigVal2 = (acSum - sqrtDiscriminant) / 2; //A * X = lambda * X => y / x = b / (lambda - c); where lambda is the first eigen-value var angle = System.Math.Atan2(2 * b, (acDiff + sqrtDiscriminant)); success = eigVal1 > 0 && eigVal2 > 0; return new Ellipse { Center = center, Size = new SizeF { Width = (float)System.Math.Sqrt(eigVal1) * 4, Height =(float)System.Math.Sqrt(eigVal2) * 4 }, Angle = (float)(angle * 180 / Math.PI) }; }
/// <summary> /// Produces a Point structure from a PointF structure by /// truncating the X and Y properties. /// </summary> /// <param name="value">Floating-point coordinate pair.</param> /// <returns>Integer coordinate pair.</returns> public static Point Truncate(PointF value) { int x, y; checked { x = (int)value.X; y = (int)value.Y; } return new Point(x, y); }
private static LineSegment2DF getLine(int derivativeOrientation, PointF centerPoint, float length) { Vector2D vec = new Vector2D(Angle.ToRadians(derivativeOrientation)).Multiply(length / 2); var p1 = vec.Add(centerPoint); var p2 = vec.Negate().Add(centerPoint); return new LineSegment2DF(p1.ToPoint(), p2.ToPoint()); }
private static Rectangle createRect(PointF centerPoint, Size size, Size frameSize) { return new Rectangle((int)centerPoint.X - size.Width / 2, (int)centerPoint.Y - size.Height / 2, size.Width, size.Height).Intersect(frameSize); }
/// <summary> /// Determines if the specified point is contained within this RectangleF structure. /// </summary> /// <param name="pt">The point to test.</param> /// <returns>This method returns true if the point represented by <paramref name="pt"/> is contained within this RectangleF structure; otherwise false.</returns> public bool Contains(PointF pt) { return Contains(pt.X, pt.Y); }
/// <summary> /// Translates a PointF by the positive of a specified size. /// </summary> /// <param name="pt">Point.</param> /// <param name="sz">Offset.</param> /// <returns>PointF structure.</returns> public static PointF Add(PointF pt, Size sz) { return new PointF(pt.X + sz.Width, pt.Y + sz.Height); }
/// <summary> /// Adjusts the location of this rectangle by the specified amount. /// </summary> /// <param name="pos">Amount to offset the location.</param> public void Offset(PointF pos) { Offset(pos.X, pos.Y); }
/// <summary> /// Translates the position of the model. /// </summary> /// <param name="state">Current state.</param> /// <param name="offset">Position offset.</param> /// <returns>New state.</returns> public static PerspectiveProjection2DModel Translate(this PerspectiveProjection2DModel state, PointF offset) { var newState = state.Clone(); newState.ImagePosition.X += offset.X; newState.ImagePosition.Y += offset.Y; return newState; }
/// <summary> /// Translates a PointF by the negative of a specified size. /// </summary> /// <param name="pt">Point.</param> /// <param name="sz">Offset.</param> /// <returns>PointF structure.</returns> public static PointF Subtract(PointF pt, SizeF sz) { return(new PointF(pt.X - sz.Width, pt.Y - sz.Height)); }