Advertisement
BanyRule

Clipping Poligon CS

Jan 12th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. namespace Clipping
  9. {
  10.     class Program
  11.     {
  12.  
  13.         struct Point
  14.         {
  15.             public double x, y;
  16.             public Point(double px, double py)
  17.             {
  18.                 x = px;
  19.                 y = py;
  20.             }
  21.         }
  22.  
  23.         static bool inside(Point p, Point cp1, Point cp2)
  24.         {
  25.             return (cp2.x - cp1.x) * (p.x - cp1.y) > (cp2.y - cp1.y) * (p.x - cp1.x);
  26.         }
  27.  
  28.         static Point intersection(Point cp1, Point cp2, Point s, Point e)
  29.         {
  30.             Point dc = new Point(cp1.x - cp2.x, cp1.y - cp2.y);
  31.             Point dp = new Point(s.x - e.x, s.y - e.y);
  32.             double n1 = cp1.x * cp2.y - cp1.y * cp2.x;
  33.             double n2 = s.x * e.y - s.y * e.x;
  34.             double n3 = 1.0 / (dc.x * dp.y - dc.y * dp.x);
  35.  
  36.             return new Point((n1 * dp.x - n2 * dc.x) * n3, (n1 * dp.y - n2 * dc.y) * n3);
  37.         }
  38.  
  39.         static void Main(string[] args)
  40.         {
  41.             List<Point> subjectPolypon = new List<Point>();
  42.             subjectPolypon.Add(new Point(50, 150));
  43.             subjectPolypon.Add(new Point(200, 50));
  44.             subjectPolypon.Add(new Point(350, 150));
  45.             subjectPolypon.Add(new Point(350, 300));
  46.             subjectPolypon.Add(new Point(250, 300));
  47.             subjectPolypon.Add(new Point(200, 250));
  48.             subjectPolypon.Add(new Point(150, 350));
  49.             subjectPolypon.Add(new Point(100, 250));
  50.             subjectPolypon.Add(new Point(100, 200));
  51.  
  52.  
  53.             List<Point> clipPolypon = new List<Point>();
  54.             clipPolypon.Add(new Point(100, 100));
  55.             clipPolypon.Add(new Point(300, 100));
  56.             clipPolypon.Add(new Point(300, 300));
  57.             clipPolypon.Add(new Point(100, 300));
  58.  
  59.             List<Point> clippingOut = subjectPolypon.ToList();
  60.             List<Point> inputList = subjectPolypon.ToList();
  61.             Point cp1 = clipPolypon.Last<Point>();
  62.  
  63.             Point temp = new Point(1, 1);
  64.  
  65.             foreach (Point clipNode in clipPolypon)
  66.             {
  67.                 Point cp2 = clipNode;
  68.                 inputList = clippingOut.ToList();
  69.                 clippingOut.Clear();
  70.                 Point s = inputList.Last();
  71.  
  72.                 foreach (Point subjectNode in inputList)
  73.                 {
  74.                     Point e = subjectNode;
  75.  
  76.                     if (inside(e, cp1, cp2))
  77.                     {
  78.                         if (!inside(s, cp1, cp2))
  79.                         {
  80.                             clippingOut.Add(intersection(cp1, cp2, s, e));
  81.                         }
  82.                         clippingOut.Add(intersection(cp1, cp2, s, e));
  83.                     }
  84.                     else if (inside(s, cp1, cp2))
  85.                     {
  86.                         clippingOut.Add(intersection(cp1, cp2, s, e));
  87.                     }
  88.  
  89.                     s = e;
  90.  
  91.                 }
  92.  
  93.                 cp1 = cp2;
  94.  
  95.             }
  96.  
  97.             Console.WriteLine(clippingOut.Count);
  98.  
  99.             Console.ReadKey();
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement