Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Clipping
- {
- class Program
- {
- struct Point
- {
- public double x, y;
- public Point(double px, double py)
- {
- x = px;
- y = py;
- }
- }
- static bool inside(Point p, Point cp1, Point cp2)
- {
- return (cp2.x - cp1.x) * (p.x - cp1.y) > (cp2.y - cp1.y) * (p.x - cp1.x);
- }
- static Point intersection(Point cp1, Point cp2, Point s, Point e)
- {
- Point dc = new Point(cp1.x - cp2.x, cp1.y - cp2.y);
- Point dp = new Point(s.x - e.x, s.y - e.y);
- double n1 = cp1.x * cp2.y - cp1.y * cp2.x;
- double n2 = s.x * e.y - s.y * e.x;
- double n3 = 1.0 / (dc.x * dp.y - dc.y * dp.x);
- return new Point((n1 * dp.x - n2 * dc.x) * n3, (n1 * dp.y - n2 * dc.y) * n3);
- }
- static void Main(string[] args)
- {
- List<Point> subjectPolypon = new List<Point>();
- subjectPolypon.Add(new Point(50, 150));
- subjectPolypon.Add(new Point(200, 50));
- subjectPolypon.Add(new Point(350, 150));
- subjectPolypon.Add(new Point(350, 300));
- subjectPolypon.Add(new Point(250, 300));
- subjectPolypon.Add(new Point(200, 250));
- subjectPolypon.Add(new Point(150, 350));
- subjectPolypon.Add(new Point(100, 250));
- subjectPolypon.Add(new Point(100, 200));
- List<Point> clipPolypon = new List<Point>();
- clipPolypon.Add(new Point(100, 100));
- clipPolypon.Add(new Point(300, 100));
- clipPolypon.Add(new Point(300, 300));
- clipPolypon.Add(new Point(100, 300));
- List<Point> clippingOut = subjectPolypon.ToList();
- List<Point> inputList = subjectPolypon.ToList();
- Point cp1 = clipPolypon.Last<Point>();
- Point temp = new Point(1, 1);
- foreach (Point clipNode in clipPolypon)
- {
- Point cp2 = clipNode;
- inputList = clippingOut.ToList();
- clippingOut.Clear();
- Point s = inputList.Last();
- foreach (Point subjectNode in inputList)
- {
- Point e = subjectNode;
- if (inside(e, cp1, cp2))
- {
- if (!inside(s, cp1, cp2))
- {
- clippingOut.Add(intersection(cp1, cp2, s, e));
- }
- clippingOut.Add(intersection(cp1, cp2, s, e));
- }
- else if (inside(s, cp1, cp2))
- {
- clippingOut.Add(intersection(cp1, cp2, s, e));
- }
- s = e;
- }
- cp1 = cp2;
- }
- Console.WriteLine(clippingOut.Count);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement