public static int[] getContourCurvatureIndices(Contour<Point> contour, Point[] curvePoints) { int[] curveIndices = new int[curvePoints.Count()]; for (int j = 0; j < curvePoints.Count(); j++) { curveIndices[j] = Array.IndexOf(contour.ToArray(), curvePoints[j]); Console.WriteLine(curveIndices[j] + ":" + curvePoints[j].ToString()); } return curveIndices; }
Point[] getStarPoints() { Point[] starPoints = new Point[5]; for (int i = 0; i < starPoints.Count(); i++) { double x, y; x = (radius * Math.Cos(-Math.PI / 2 - i * 2 * Math.PI / starPoints.Count())); y = (radius * Math.Sin(-Math.PI / 2 - i * 2 * Math.PI / starPoints.Count())); starPoints[i] = new Point((int)x, (int)y); } return starPoints; }
public static double[] getLinearRegressionData(Point[] dataPoints) { int sumX = 0; int sumY = 0; int sumXY = 0; int sumX2 = 0; int n = dataPoints.Count(); // x will be equal to the last Point.X, find Point.Y foreach (Point p in dataPoints) { sumX += p.X; sumY += p.Y; sumXY += p.X * p.Y; sumX2 += p.X * p.X; } // intercept double a = ((sumY * sumX2) - (sumX * sumXY)) / ((n * sumX2) - (sumX * sumX)); // slope double b = ((n * sumXY) - (sumX * sumY)) / ((n * sumX2) - (sumX * sumX)); return new double[2]{a, b}; }
private static int calculateMedianYCoordinate(Point[] locations) { int[] orderedYCoordinatesOfLocations = locations.Select(location => location.Y).OrderBy(yCoordinate => yCoordinate).ToArray(); int medianIndex = locations.Count() / 2; int medianY = orderedYCoordinatesOfLocations[medianIndex]; return medianY; }
/// <summary> /// コンストラクタ /// </summary> /// <param name="points">通過点</param> public Polyline(Point[] points) { if (points.Count() < 2) { throw new ArgumentOutOfRangeException("2点以上必要です。"); } Points = points; }
internal void DrawPolygon(Color color, Point[] vertices) { StreamGeometry streamGeometry = new StreamGeometry(); using (StreamGeometryContext geometryContext = streamGeometry.Open()) { geometryContext.BeginFigure(vertices[0], true, true); //TODO check if this can be made in a more safe way the [0] PointCollection points = new PointCollection(); for (int i = 1; i < vertices.Count();i++) { points.Add(vertices[i]); } geometryContext.PolyLineTo(points, true, true); } // Draw the polygon visual DrawingContext drawingContext = drawingVisual.RenderOpen(); drawingContext.DrawGeometry(mySolidColorBrush, null, streamGeometry); drawingContext.Close(); image.Render(drawingVisual); }
private static void CreateVertexColoredMesh(Point[] vertices, Color[] colors, IRenderPackage package, TessellationParameters parameters) { package.RequiresPerVertexColoration = true; for (var i = 0; i <= vertices.Count()-3; i+=3) { var ptA = vertices[i]; var ptB = vertices[i+1]; var ptC = vertices[i+2]; if (ptA.IsAlmostEqualTo(ptB) || ptB.IsAlmostEqualTo(ptC) || ptA.IsAlmostEqualTo(ptC)) { continue; } var alongLine = false; using (var l = Line.ByStartPointEndPoint(ptA, ptC)) { alongLine = ptB.DistanceTo(l) < 0.00001; } if (alongLine) { continue; } var cA = colors[i]; var cB = colors[i+1]; var cC = colors[i+2]; var s1 = ptB.AsVector().Subtract(ptA.AsVector()).Normalized(); var s2 = ptC.AsVector().Subtract(ptA.AsVector()).Normalized(); var n = s1.Cross(s2); package.AddTriangleVertex(ptA.X, ptA.Y, ptA.Z); package.AddTriangleVertexNormal(n.X, n.Y, n.Z); package.AddTriangleVertexColor(cA.Red, cA.Green, cA.Blue, cA.Alpha); package.AddTriangleVertexUV(0, 0); package.AddTriangleVertex(ptB.X, ptB.Y, ptB.Z); package.AddTriangleVertexNormal(n.X, n.Y, n.Z); package.AddTriangleVertexColor(cB.Red, cB.Green, cB.Blue, cB.Alpha); package.AddTriangleVertexUV(0, 0); package.AddTriangleVertex(ptC.X, ptC.Y, ptC.Z); package.AddTriangleVertexNormal(n.X, n.Y, n.Z); package.AddTriangleVertexColor(cC.Red, cC.Green, cC.Blue, cC.Alpha); package.AddTriangleVertexUV(0, 0); } }
private List<AForge.IntPoint> convertToIntPoint(Point[] systemPoints) { List<AForge.IntPoint> intPoints = new List<AForge.IntPoint>(systemPoints.Count()); foreach (Point p in systemPoints) { intPoints.Add(new AForge.IntPoint(p.X, p.Y)); } return intPoints; }
public void DetectFingers(Image<Gray, Byte> image) { /* Point3D ptHandProjective = this.depth.ConvertRealWorldToProjective(ptHand); Size offset = new Size((int)ptHandProjective.X, (int)ptHandProjective.Y); //get the hand Image<Gray, Byte> image = detector.DetectHand(this.depth, ptHand); */ //image = image.PyrDown().PyrUp(); //image._SmoothGaussian(5); //image._ThresholdBinary(new Gray(149), new Gray(255)); Graphics g = Graphics.FromImage(this.bitmap); MemStorage storage1 = new MemStorage(); Contour<Point> contours = new Contour<Point>(storage1); // Find the biggest contour for (Contour<Point> tempContours = image.FindContours(); tempContours != null; tempContours = tempContours.HNext) { if (tempContours.Area > contours.Area) contours = tempContours; } //Console.WriteLine(contours.Area); if (contours.Area != 0) { List<KeyValuePair<Point, bool>> significantPts = new List<KeyValuePair<Point, bool>>(); Rectangle contourRectangle = contours.BoundingRectangle; g.DrawRectangle(new Pen(Brushes.Green), contourRectangle); //Seq<Point> contoursHull = contours.GetConvexHull(Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE); // Convert the set of contour points to a polygon for graphical representation Seq<Point> poly = contours.ApproxPoly(20); Point[] polygon = poly.ToArray(); PointF[] contourF = Array.ConvertAll(contours.ToArray(), new Converter<Point, PointF>(PointToPointF)); //CircleF palmCircle = PointCollection.MinEnclosingCircle(contourF); //CircleF palmCircle = PointCollection.EllipseLeastSquareFitting(contourF); //MCvBox2D box = PointCollection.MinAreaRect(contourF); //g.DrawRectangle(new Pen(Brushes.Aquamarine),(Rectangle) box.MinAreaRect()); //drawCircle(g, new Pen(Brushes.Green), PointFToPoint(palmCircle.Center), (int) Math.Round(palmCircle.Radius)); // Get the convex hull based on the contour polygon Seq<Point> convexHull = poly.GetConvexHull(Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE); /* * Handle opening and closing of the fist each frame. * Fisting gesture controls the left mouse button. * Based on the ratio of area between the convex and the hull. * (See calculation above) Threshold currently set to 0.8 * * NEED TO HAND COORDINATES */ double hullArea = convexHull.Area; double contourArea = contours.Area; double currentHullRatio = contourArea / hullArea; // newest ratio is in front of array hullRatios[2] = hullRatios[1]; hullRatios[1] = hullRatios[0]; hullRatios[0] = currentHullRatio; this.fixedHullRatio = hullRatios[2]; if (smoothHullRatios && this.frame > 3) { this.hullRatios = smoothHullRatioArray(this.hullRatios); } bool fistClosed = true; stopTracking = true; if (shouldHandClose()) { stopTracking = true; fistClosed = false; } if (shouldControlMouse) { //clickMouse(fistClosed); } // Get the convexity defects from the contour stopwatch.Restart(); Seq<Emgu.CV.Structure.MCvConvexityDefect> contourDefects = contours.GetConvexityDefacts(storage1, Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE); stopwatch.Stop(); //Console.WriteLine(Math.Round(1000.0 * (double)stopwatch.ElapsedTicks / Stopwatch.Frequency, 4) + ", GetConvexityDefacts()"); // Draw the polygon that was converted from the contour /* for (int i = 0; i < polygon.Length; i++) { if (i == polygon.Length - 1) g.DrawLine((new Pen(Brushes.Blue))), polygon[i], polygon[0]); else g.DrawLine((new Pen(Brushes.Blue))), polygon[i], polygon[i + 1]); } */ // Draw more polygon stuff ?? foreach (Point p in poly) { significantPts.Add(new KeyValuePair<Point, bool>(p, false)); //g.DrawLine((new Pen(Brushes.Blue))), p.X, p.Y, p.X + 10, p.Y + 10); } int fingertips = 0; Point[] defectPoints = new Point[contourDefects.Count()]; Point[] fingerPoints = new Point[contourDefects.Count()]; Point[] endPoints = new Point[contourDefects.Count()]; int count = 0; int fingerCount = 0; ///--- DEFECTS foreach (MCvConvexityDefect defect in contourDefects) { // Construct a triangle from the defect Triangle2DF defectTriangle = new Triangle2DF(defect.DepthPoint, defect.StartPoint, defect.EndPoint); // Check that the area of the defect triangle is between 1% and 33% of the contour area if (defectTriangle.Area > contours.Area / 100 && defectTriangle.Area < contours.Area / 3) { ///--- endPoints[count] = defect.EndPoint; defectPoints[count] = defect.DepthPoint; count++; // Get the angle of the defect double defectAngle = getVertexAngle(defect.DepthPoint, defect.StartPoint, defect.EndPoint); if (defectAngle < 100 && !defect.EndPoint.Equals(Point.Empty)) fingerPoints[fingerCount++] = defect.EndPoint; ///--- //g.DrawPolygon(new Pen(Brushes.Pink), defectTriangle.GetVertices()); significantPts.Add(new KeyValuePair<Point, bool>(defect.DepthPoint, true)); g.DrawLine((new Pen(Brushes.Green)), defect.StartPoint, defect.EndPoint); g.DrawLine((new Pen(Brushes.Red)), defect.DepthPoint, defect.StartPoint); g.DrawLine((new Pen(Brushes.Blue)), defect.DepthPoint, defect.EndPoint); // Draw the depth point of the defects (hull point) g.DrawRectangle(new Pen(Brushes.Green), new Rectangle(Point.Subtract(defect.DepthPoint, new Size(5, 5)), new Size(10, 10))); // Check if angle is obtuse or acute g.DrawString(defectAngle.ToString(), new Font(FontFamily.GenericSerif,5), Brushes.Blue, defect.DepthPoint.X + 5, defect.DepthPoint.Y + 5); /* if (defectAngle < 70) drawCircle(g,new Pen(Brushes.White, 2), defect.StartPoint, 5); else if (defectAngle < 100) { drawCircle(g, new Pen(Brushes.White, 2), defect.StartPoint, 5); drawCircle(g, new Pen(Brushes.White, 2), defect.EndPoint, 5); } else drawCircle(g, new Pen(Brushes.Gray, 2), defect.StartPoint, 5); */ } } ///--- ///--- K-CURVATURE stopwatch.Restart(); //Point[] curvePoints = endPoints.ToArray(); Point[] curvePoints = convexHull.ToArray(); //int[] indexArray = getContourCurvatureIndices(contours, curvePoints); int[] indexArray = KCurvature.getContourCurvatureIndices(contours, curvePoints); //KCurve1(contours, indexArray, g); //KCurve2(contours, indexArray, g); Accord.Math.Geometry.KCurvature kcurve = new Accord.Math.Geometry.KCurvature(16, new AForge.DoubleRange(30, 90)); List<AForge.IntPoint> intPointContour = convertToIntPoint(contours.ToArray()); List<AForge.IntPoint> contourPeaks = kcurve.FindPeaks(intPointContour); this.minFingerDistance = getMinDistance(contourPeaks.ToArray()); ///--- /// Trying to find the forearm stopwatch.Restart(); /// /// method 1 /// Point[] forearmPoints = new Point[convexHull.Count()]; int k = 0; foreach (Point p in convexHull.ToArray()) { double error = 15; if (Math.Abs(p.Y - contours.BoundingRectangle.Bottom) < error) { //g.DrawRectangle(new Pen(Brushes.Aquamarine), p.X, p.Y, 5, 5); forearmPoints[k] = p; //Console.WriteLine("point {1}: {0}",p.ToString(), k); k++; } } Point p0; //Console.WriteLine("forearm points: {0}", forearmPoints.Count()); if (forearmPoints.Count() == 1) p0 = forearmPoints[0]; else p0 = getMidPoint(forearmPoints[0], forearmPoints[1]); //Console.WriteLine("midpoint : {0}", p0.ToString()); g.DrawRectangle(new Pen(Brushes.Aquamarine), p0.X, p0.Y, 5, 5); g.DrawLine(new Pen(Brushes.Aquamarine),p0 ,convertPoint3D(lastPoint)); /// /// /// /* //Point[] possibleWristPoints = convexHull.Intersect(contours).ToArray(); Point[] possibleWristPoints = convexHull.ToArray(); for (int i = 0; i < possibleWristPoints.Count(); i++) { Point p = possibleWristPoints[i]; double error = 10; if (Math.Abs(p.Y - contours.BoundingRectangle.Bottom) < error) g.DrawRectangle(new Pen(Brushes.Aquamarine), p.X, p.Y, 10, 10); double minDistance = 1000000; for (int j = 0; j < contourPeaks.Count(); j++) { AForge.IntPoint p2 = contourPeaks[j]; double pointDistance = getPointDistance(p, new Point(p2.X, p2.Y)); if (pointDistance < minDistance) minDistance = pointDistance; } //if (minDistance > 50) //g.DrawRectangle(new Pen(Brushes.Aquamarine), p.X, p.Y, 10, 10); //LineSegment2D line = new LineSegment2D(possibleWristPoints[i], possibleWristPoints[i + 1]); //Point midPoint = getMidPoint(possibleWristPoints[i], possibleWristPoints[i + 1]); //if (contourRectangle.Bottom == midPoint.X) //if (contours.Contains(line.P1) && contours.Contains(line.P2)) //g.DrawLine(new Pen(Brushes.Aquamarine), possibleWristPoints[i], possibleWristPoints[i + 1]); //g.DrawString(i.ToString(), DefaultFont, Brushes.Red, p.X, p.Y); //Console.WriteLine("Possible wrist point: {0}, {1}", p.X, p.Y); //i++; } */ stopwatch.Stop(); //Console.WriteLine("polygon/contour intersect time: {0}", Math.Round(1000.0 * (double)stopwatch.ElapsedTicks / Stopwatch.Frequency, 4)); ///--- /// // Count the finger tips found if we've processed 3 frames already if (frame > fingersDetectedHistorySize) { for (int i = 0; i < fingersDetectedHistorySize - 1; i++) { //Console.WriteLine("finger history {0}: {1}", i, fingersDetectedHistory[i]); fingersDetectedHistory[i] = fingersDetectedHistory[i + 1]; } fingersDetectedHistory[fingersDetectedHistorySize - 1] = contourPeaks.Count() - 1; if (fingersDetectedHistory[fingersDetectedHistorySize-1] == fingersDetectedHistory[fingersDetectedHistorySize-3] && fingersDetectedHistory[fingersDetectedHistorySize-3] != fingersDetectedHistory[fingersDetectedHistorySize-2]) fingersDetectedHistory[fingersDetectedHistorySize-2] = fingersDetectedHistory[fingersDetectedHistorySize-3]; //Console.WriteLine("Fingers: {0}", fingersDetectedHistory[0]); } if (this.startClickLag) this.clickLag++; double perimeterMultiplier = (this.lastPoint.Z / 800); double contourPerimeter = contours.Perimeter * perimeterMultiplier; int fingers = contourPeaks.Count - 1; // contourPerimeter > 700 is an experiemental value obtained my analyzing my own // hand contour. this must be changed for other users. //if ((contourPerimeter > 600 || fingers > 3) && !this.leftButtonDown) if ((fingers > 3) && !this.leftButtonDown) { if (this.clickLag < 7) this.clickLag++; else { stopTracking = false; this.clickLag = 0; } /* if (this.startClickLag && !mouseActionReset) { if (this.clickLag < 20) { stopTracking = true; this.clickLag++; } else { this.clickLag = 0; this.startClickLag = false; } } else stopTracking = true; * */ } // this doesnt account for releasing the left mouse button if (this.frame > 5 && shouldControlMouse && fingersDetectedHistory[0] == fingersDetectedHistory[1] && fingersDetectedHistory[1] == fingersDetectedHistory[2]) { if (!leftButtonDown) stopTracking = true; if (fingersDetectedHistory[0] == 1) { this.frameCount++; if (this.frameCount > 25) sendMouseEvent(11); } else { if (this.frameCount > 3 && this.frameCount < 25) sendMouseEvent(1); else if (this.frameCount > 25) sendMouseEvent(11); else sendMouseEvent(fingersDetectedHistory[0]); this.frameCount = 0; } } // Highlight the finger tip positions int peakIndex = 1; foreach (AForge.IntPoint p in contourPeaks) { g.DrawRectangle(new Pen(Brushes.White), p.X - 2, p.Y - 2, 4, 4); g.DrawString(peakIndex.ToString(), DefaultFont, Brushes.White, p.X + 4, p.Y - 4); peakIndex++; } stopwatch.Stop(); //Console.WriteLine("KCurve time {0} ms", Math.Round(1000.0 * (double)stopwatch.ElapsedTicks / Stopwatch.Frequency, 4)); ///--- /// // to build a circle just start with a square and filter by radius distance //Point polygonCenter = getCentroid(polygon); //Point contourCenter = getCentroid(contours.ToArray()); //updatePalm(g, contours); updatePalmNite(); /* drawCircle(g, new Pen(Brushes.BlueViolet), contourRectangleCenter, 10); drawCircle(g, new Pen(Brushes.BlueViolet), contourRectangleCenter, 25); drawCircle(g, new Pen(Brushes.BlueViolet), contourRectangleCenter, 50); */ //Console.WriteLine(this.lastPoint.X + ", " + this.lastPoint.Y + ": " + depth.GetMetaData()[(int)this.lastPoint.X, (int)this.lastPoint.Y]); g.DrawString("now " + (contourPeaks.Count() - 1).ToString(), DefaultFont, Brushes.White, 50, 130); g.DrawString("filtered " + fingersDetectedHistory[0].ToString(), DefaultFont, Brushes.White, 50, 150); g.DrawString(Math.Round(this.hullRatios[2], 4).ToString(), DefaultFont, Brushes.White, 50, 170); g.DrawString("handOpen:"+fistClosed.ToString(), DefaultFont, Brushes.White, 50, 190); g.DrawString("Frames: "+this.frame, DefaultFont, Brushes.White, 50, 210); if (shouldControlMouse && !stopTracking) g.DrawString("Cursor: " + lastScaledMousePoint.X+", "+lastScaledMousePoint.Y, DefaultFont, Brushes.White, 50, 230); g.DrawString(depth.GetMetaData().FPS.ToString(), DefaultFont, Brushes.White, 50, 250); g.DrawString(this.minFingerDistance.ToString(), DefaultFont, Brushes.White, 50, 270); //g.DrawClosedCurve(new Pen(Brushes.Purple), fingerPoints); //Save the frame to disk (if specified) if (this.captureFrames) this.bitmap.Save(System.IO.Path.Combine(this.frameDir,this.frame + ".png"), ImageFormat.Png); this.frame++; } }
private Point getCentroid(Point[] points) { int x = 0; int y = 0; foreach (Point p in points) { x += p.X; y += p.Y; } x /= points.Count(); y /= points.Count(); return new Point((int)x, (int)y); }
internal static void ApplyAnimationToBezierSegments(Int32 index, Storyboard storyBoard, BezierSegment bezierSegment, Point[] oldCtrlPoints, Point[] newCtrlPoints, PathFigure pathFigure, Point oldFirstPointOfBezierSeg, Point newFirstPointOfBezierSeg) { if (index == 1) { PointAnimation pointAnimation = new PointAnimation() { From = oldFirstPointOfBezierSeg, To = newFirstPointOfBezierSeg, SpeedRatio = 2, Duration = new Duration(new TimeSpan(0, 0, 1)) }; Storyboard.SetTarget(pointAnimation, pathFigure); Storyboard.SetTargetProperty(pointAnimation, new PropertyPath("StartPoint")); Storyboard.SetTargetName(pointAnimation, (String)pathFigure.GetValue(FrameworkElement.NameProperty)); storyBoard.Children.Add(pointAnimation); #if WPF pathFigure.BeginAnimation(PathFigure.StartPointProperty, pointAnimation); #endif } // Animate control points if (oldCtrlPoints != null && newCtrlPoints != null && oldCtrlPoints.Count() == 3 && newCtrlPoints.Count() == 3) { // Loop for 3 control points for (int i = 0; i < 3; i++) { // Creates PointAnimation for each control points if (!oldCtrlPoints[i].Equals(newCtrlPoints[i])) { PointAnimation pointAnimation = new PointAnimation() { From = oldCtrlPoints[i], To = newCtrlPoints[i], SpeedRatio = 2, Duration = new Duration(new TimeSpan(0, 0, 1)) }; Storyboard.SetTarget(pointAnimation, bezierSegment); Storyboard.SetTargetProperty(pointAnimation, new PropertyPath("Point" + (i + 1).ToString())); Storyboard.SetTargetName(pointAnimation, (String)bezierSegment.GetValue(FrameworkElement.NameProperty)); storyBoard.Children.Add(pointAnimation); #if WPF switch(i) { case 0: bezierSegment.BeginAnimation(BezierSegment.Point1Property, pointAnimation); break; case 1: bezierSegment.BeginAnimation(BezierSegment.Point2Property, pointAnimation); break; case 2: bezierSegment.BeginAnimation(BezierSegment.Point3Property, pointAnimation); break; } #endif } } } }
private bool PangData(ref Point ptStart, ref Point ptEnd) { for (int i = 2; i < 9; i++) { for (int j = 2; j < 9; j++) { Animals iCurrentAni = m_pMainData[i, j]; Point[] pIndex = new Point[8]; Point ptCenter = new Point(); pIndex[0] = new Point(j - 1, i - 1); pIndex[1] = new Point(j, i - 1); pIndex[2] = new Point(j + 1, i - 1); pIndex[3] = new Point(j - 1, i); ptCenter = new Point(j, i); pIndex[4] = new Point(j + 1, i); pIndex[5] = new Point(j - 1, i + 1); pIndex[6] = new Point(j, i + 1); pIndex[7] = new Point(j + 1, i + 1); int iIndexCount = pIndex.Count(); for (int x = 0; x < iIndexCount; x++) { if (m_pMainData[ptCenter.X, ptCenter.Y] == m_pMainData[pIndex[x].X, pIndex[x].Y]) { int iIndex = x; switch (iIndex) { case 0: if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y - 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y - 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X, ptCenter.Y, ptCenter.X - 1, ptCenter.Y); return(true); } else if ((m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y - 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y - 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X - 1, ptCenter.Y - 1, ptCenter.X, ptCenter.Y - 1); return(true); } } break; case 1: if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { if ((m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y - 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y - 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X - 1, ptCenter.Y - 2, ptCenter.X, ptCenter.Y - 2); return(true); } else if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y - 2] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y - 2] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X, ptCenter.Y - 3, ptCenter.X, ptCenter.Y - 2); return(true); } else if ((m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y - 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y - 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X + 1, ptCenter.Y - 2, ptCenter.X, ptCenter.Y - 2); return(true); } } break; case 2: if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y - 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y - 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X, ptCenter.Y, ptCenter.X + 1, ptCenter.Y); return(true); } else if ((m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y - 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y - 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X + 1, ptCenter.Y - 1, ptCenter.X, ptCenter.Y - 1); return(true); } } break; case 3: if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { if ((m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y - 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y - 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X - 2, ptCenter.Y - 1, ptCenter.X - 2, ptCenter.Y); return(true); } else if ((m_pMainData[pIndex[iIndex].X - 2, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X - 2, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X - 3, ptCenter.Y, ptCenter.X - 2, ptCenter.Y); return(true); } else if ((m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y + 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y + 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X - 2, ptCenter.Y + 1, ptCenter.X - 2, ptCenter.Y); return(true); } } break; case 4: if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { if ((m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y - 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y - 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X + 2, ptCenter.Y - 1, ptCenter.X + 2, ptCenter.Y); return(true); } else if ((m_pMainData[pIndex[iIndex].X + 2, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X + 2, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X + 3, ptCenter.Y, ptCenter.X + 2, ptCenter.Y); return(true); } else if ((m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y + 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y + 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X + 2, ptCenter.Y + 1, ptCenter.X + 2, ptCenter.Y); return(true); } } break; case 5: if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y + 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y + 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X, ptCenter.Y, ptCenter.X - 1, ptCenter.Y); return(true); } else if ((m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y + 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y + 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X - 1, ptCenter.Y + 1, ptCenter.X, ptCenter.Y + 1); return(true); } } break; case 6: if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { if ((m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y + 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y + 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X - 1, ptCenter.Y + 2, ptCenter.X, ptCenter.Y + 2); return(true); } else if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y + 2] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y + 2] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X, ptCenter.Y + 3, ptCenter.X, ptCenter.Y + 2); return(true); } else if ((m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y + 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X + 1, pIndex[iIndex].Y + 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X + 1, ptCenter.Y + 2, ptCenter.X, ptCenter.Y + 2); return(true); } } break; case 7: if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y] == m_pMainData[ptCenter.X, ptCenter.Y])) { if ((m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y + 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X, pIndex[iIndex].Y + 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X, ptCenter.Y, ptCenter.X + 1, ptCenter.Y); return(true); } else if ((m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y + 1] != Animals.ERROR) && (m_pMainData[pIndex[iIndex].X - 1, pIndex[iIndex].Y + 1] == m_pMainData[ptCenter.X, ptCenter.Y])) { PointSetting(ref ptStart, ref ptEnd, ptCenter.X + 1, ptCenter.Y + 1, ptCenter.X, ptCenter.Y + 1); return(true); } } break; } } } if (iCurrentAni == Animals.BOMB) //폭탄 { ptStart = new Point(i, j); ptEnd = new Point(i, j); PointSetting(ref ptStart, ref ptEnd, ptStart.X, ptStart.Y, ptEnd.X, ptEnd.Y); return(true); } } } return(false); }
public static double getLinearRegressionSlope(Point[] dataPoints) { int sumX = 0; int sumY = 0; int sumXY = 0; int sumX2 = 0; int n = dataPoints.Count(); // x will be equal to the last Point.X, find Point.Y foreach (Point p in dataPoints) { sumX += p.X; sumY += p.Y; sumXY += p.X * p.Y; sumX2 += p.X * p.X; } // slope double b = ((n * sumXY) - (sumX * sumY)) / ((n * sumX2) - (sumX * sumX)); return b; }
private bool polyHitTest(Point[] vertices, Point testPoint) { int i, j; bool c = false; for (i = 0, j = vertices.Count() - 1; i < vertices.Count(); j = i++) { if (((vertices[i].Y > testPoint.Y) != (vertices[j].Y > testPoint.Y)) && (testPoint.X < (vertices[j].X - vertices[i].X) * (testPoint.Y - vertices[i].Y) / (vertices[j].Y - vertices[i].Y) + vertices[i].X)) c = !c; } return c; }
public static bool PolygonContainsPoint(Point[] polygon, Point point) { bool result = false; int j = polygon.Count() - 1; for (int i = 0; i < polygon.Count(); i++) { if (polygon[i].Y < point.Y && polygon[j].Y >= point.Y || polygon[j].Y < point.Y && polygon[i].Y >= point.Y) { if (polygon[i].X + (point.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < point.X) { result = !result; } } j = i; } return result; }
Tree CreateTree(Color color, Point root, Point[] leaves, Point[] parents) { int branchLength = presentTreeTool.branchLength; int maxGeneration = presentTreeTool.maxGeneration; int branchWidth = presentTreeTool.branchWidth; int hullWidth = presentTreeTool.hullWidth; int leafSize = presentTreeTool.leafSize; //TODO change to GetType and Activator instead of switch Tree tree = new PolyTree(color, root, branchLength, leaves.Count(), parents, leaves, branchWidth, hullWidth, leafSize); switch (presentTreeTool.renderObjectName) { case "PolyTree": tree = new PolyTree(color, root, branchLength, leaves.Count(), parents, leaves, branchWidth, hullWidth, leafSize); break; case "WoolTree": tree = new WoolTree(color, root, branchLength, leaves.Count(), parents, leaves, branchWidth, hullWidth, leafSize); break; case "CellNetTree": tree = new CellNetTree(color, root, branchLength, leaves.Count(), parents, leaves, branchWidth, hullWidth, leafSize); break; case "ModernArtTree": tree = new ModernArtTree(color, root, branchLength, leaves.Count(), parents, leaves, branchWidth, hullWidth, leafSize); break; case "BubbleTree": tree = new BubbleTree(color, root, branchLength, leaves.Count(), parents, leaves, branchWidth, hullWidth, leafSize); break; case "ScribbleTree": tree = new ScribbleTree(color, root, branchLength, leaves.Count(), parents, leaves, branchWidth, hullWidth, leafSize); break; default: break; } return tree; }
/** * Translate the data points so as to set the start point as the origin. * Probably unnecessary but easier to work with at this point */ public static Point[] translateDataPoints(Point[] dataPoints, Point startPoint) { Point[] translatedDataPoints = new Point[dataPoints.Count()]; for (int i = 0; i < dataPoints.Count(); i++) { Point p = dataPoints[i]; translatedDataPoints[i] = new Point(p.X - startPoint.X, p.Y - startPoint.Y); } return translatedDataPoints; }
//Allow the user to import an array of points to be used to draw a signature in the view, with new //lines indicated by a PointF.Empty in the array. public void LoadPoints (Point [] loadedPoints) { if (loadedPoints == null || loadedPoints.Count () == 0) return; var startIndex = 0; var emptyIndex = loadedPoints.ToList ().IndexOf (new Point (-10000, -10000)); if (emptyIndex == -1) emptyIndex = loadedPoints.Count (); //Clear any existing paths or points. strokes = new List<Stroke> (); points = new List<Point []> (); do { //Create a new path and set the line options currentStroke = new Stroke (); currentStroke.DrawingAttributes.Color = strokeColor; currentStroke.DrawingAttributes.Width = lineWidth; currentStroke.DrawingAttributes.Height = lineWidth; currentPoints = new List<Point> (); //Move to the first point and add that point to the current_points array. currentStroke.StylusPoints.Add (GetPoint (loadedPoints [startIndex])); currentPoints.Add (loadedPoints [startIndex]); //Iterate through the array until an empty point (or the end of the array) is reached, //adding each point to the current_path and to the current_points array. for (var i = startIndex + 1; i < emptyIndex; i++) { currentStroke.StylusPoints.Add (GetPoint (loadedPoints [i])); currentPoints.Add (loadedPoints [i]); } //Add the current_path and current_points list to their respective Lists before //starting on the next line to be drawn. strokes.Add (currentStroke); points.Add (currentPoints.ToArray ()); //Obtain the indices for the next line to be drawn. startIndex = emptyIndex + 1; if (startIndex < loadedPoints.Count () - 1) { emptyIndex = loadedPoints.ToList ().IndexOf (new Point (-10000, -10000), startIndex); if (emptyIndex == -1) emptyIndex = loadedPoints.Count (); } else emptyIndex = startIndex; } while (startIndex < emptyIndex); //Obtain the image for the imported signature and display it in the image view. image.Source = GetImage (false); //Display the clear button. btnClear.Visibility = Visibility.Visible; }
public void ProdDrawPloygon(ProdInfo prodinfo, ref Bitmap bt, int heightId, PictureBox p1, int xuhao) { int count = prodinfo.Cut.Count; SetRation(prodinfo); Point xtop = new Point(OrigninP.X, OrigninP.Y); if (bt == null) { bt = new Bitmap(OrigninP.X + len + xmargin * (prodinfo.Cut.Count + 1) + 100 * xmargin + 100, 2 * OrigninP.Y + (count + 50) * (height + ymargin)); //p1.Height = bt.Height + 100; } Point[] pwl = new Point[2]; DrawLen(bt, heightId, (int)(prodinfo.Len * ration / Constant.dataMultiple), ref pwl); Point[] pArray = new Point[4]; Point[] pLine = new Point[2]; if (prodinfo.Cut.Count > 0) { xtop.X = xtop.X + calcuXMargin(prodinfo.leftAngle[0]); xtop.Y = xtop.Y + heightId * height + ymargin * heightId; pLine[0] = xtop; pLine[1] = xtop; pLine[1].Y += height; DrawLine(bt, pLine); //开始画图 for (int i = 0; i < prodinfo.Cut.Count; i++) { double upSize = 0; double downSize = 0; if (!double.TryParse(prodinfo.Param5[i], out upSize)) { MessageBox.Show(Constant.convertError + Constant.resultTip5 + heightId.ToString() + Constant.resultTip6 + Constant.resultTip5 + i.ToString() + Constant.resultTip7); return; } if (!double.TryParse(prodinfo.Param6[i], out downSize)) { MessageBox.Show(Constant.convertError + Constant.resultTip5 + heightId.ToString() + Constant.resultTip6 + Constant.resultTip5 + i.ToString() + Constant.resultTip7); return; } upSize = upSize * (ration); downSize = downSize * (ration); if (i < (prodinfo.Cut.Count() - 1)) { if (i != 0) { pArray = pointArrayGet(ref xtop, prodinfo.leftAngle[i], prodinfo.rightAngle[i], prodinfo.leftAngle[i + 1], prodinfo.rightAngle[i - 1], (int)upSize, (int)downSize); } else { pArray = pointArrayGet(ref xtop, prodinfo.leftAngle[i], prodinfo.rightAngle[i], prodinfo.leftAngle[i + 1], 90, (int)upSize, (int)downSize); } } else { if (i > 0) { pArray = pointArrayGet(ref xtop, prodinfo.leftAngle[i], prodinfo.rightAngle[i], 0, prodinfo.rightAngle[i - 1], (int)upSize, (int)downSize); } else { pArray = pointArrayGet(ref xtop, prodinfo.leftAngle[i], prodinfo.rightAngle[i], 0, 0, (int)upSize, (int)downSize); } } if (i != 0) { if (prodinfo.Barc.Contains("尾料裁剪")) { DrawPolygon(bt, pArray, 7, ""); } else { DrawPolygon(bt, pArray, 1, ""); } } else { DrawPolygon(bt, pArray, 1, PackShowStr(xuhao, prodinfo)); } } //尾料再换个颜色 List <Point> pwlLst = new List <Point>(); if (pArray.Count() == 4 && pwl.Count() == 2) { pwlLst.Add(pArray[1]); pwlLst.Add(pwl[0]); pwlLst.Add(pwl[1]); pwlLst.Add(pArray[2]); if (prodinfo.Barc.Contains("尾料裁剪")) { DrawPolygon(bt, pwlLst.ToArray(), 8, ""); } else { DrawPolygon(bt, pwlLst.ToArray(), 3, ""); } } if (bt != null) { p1.Image = bt; } } }
//returns true if point inside polygon //THIS DOES NOT BELONG HERE !!!!!!!!!!!!!!!! need a polygon utilities class!!!! shared by planner and the Waldo_FCS private bool pointInsidePolygon(Point pt, Point[] poly) { ///////////////////////////////////////////////////////////////////////////////////// //return true of the input point is insoide the polygon //nVertex number of points (vertices) in the polygpon //pt.X, pt.Y the geodetic point to test //poly[i].X, poly[i].Y the polygon vertex points //code is taken from here ........... // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html ///////////////////////////////////////////////////////////////////////////////////// // CAREFUL ///////////////////////////////////////////////////////////////////////////////// // does this make an assumption of the direction of the poly points around the polygon?? // i dont think so --- just counts semi-infinite ray crossings from point through the poly segments // odd crossings: its inside and even crossings: its outside ... ////////////////////////////////////////////////////////////////////////////////////////////// int nVertex = poly.Count(); int i, j; bool c = false; int zc = 0; for (i = 0, j = nVertex - 1; i < nVertex; j = i++) { if (((poly[i].X < pt.X) == (poly[j].X < pt.X))) continue; //test if a ray along increasing lat-direction from test point crosses this poly segment double latDiff = pt.Y - ((poly[j].Y - poly[i].Y) * (pt.X - poly[i].X) / (poly[j].X - poly[i].X) + poly[i].Y); //double lonDiff = pt.X - ((poly[j].X - poly[i].X) * (pt.Y - poly[i].Y) / (poly[j].Y - poly[i].Y) + poly[i].X); //count the crossings of the segments -- if even, on outside; if odd, on the inside if (latDiff <= 0) { c = !c; zc++; } //counts even or odd intersections of the poly } return c; }
public static Display ByPointsColors(Point[] points, Color[] colors) { if(points == null) { throw new ArgumentNullException("points"); } if (!points.Any()) { throw new ArgumentException(Resources.NoVertexExceptionMessage, "points"); } if (points.Count() %3 != 0) { throw new ArgumentException(Resources.VerticesDivisibleByThreeExceptionMessage); } if(colors == null) { throw new ArgumentNullException("colors"); } if (!colors.Any()) { throw new ArgumentException(Resources.NoColorsExceptionMessage, "colors"); } if (colors.Count() != points.Count()) { throw new ArgumentException(Resources.VertexColorCountMismatchExceptionMessage, "colors"); } return new Display(points, colors); }