Guest User

Untitled

a guest
May 26th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10.  
  11. namespace TriangleClipping
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.  
  19.             pictureBox1.Resize += new EventHandler(pictureBox1_Resize);
  20.         }
  21.  
  22.         void pictureBox1_Resize(object sender, EventArgs e)
  23.         {
  24.             pictureBox1.Invalidate();
  25.         }
  26.  
  27.         private void pictureBox1_Click(object sender, EventArgs e)
  28.         {
  29.             _clickPosition = PointToClient(Cursor.Position);
  30.             pictureBox1.Invalidate();
  31.         }
  32.  
  33.         Point _clickPosition = new Point(128 / 2, 128 / 2);
  34.  
  35.         public static Point RotatePoint(PointF p, PointF p2, double angle)
  36.         { //angle in radians.
  37.             return new Point((int)(Math.Cos(angle) * (p.X - p2.X) - Math.Sin(angle) * (p.Y - p2.Y) + p2.X), (int)(Math.Sin(angle) * (p.X - p2.X) + Math.Cos(angle) * (p.Y - p2.Y) + p2.Y));
  38.         }
  39.         public static double DegreeToRadian(double angle)
  40.         {
  41.             return Math.PI * angle / 180.0;
  42.         }
  43.         public static double RadianToDegree(double angle)
  44.         {
  45.             return angle * 180.0 / Math.PI;
  46.         }
  47.  
  48.         public static Point[] Triangle(int w, int h, int angle)
  49.         {
  50.             Point first = RotatePoint(new Point(w, h), new Point(w / 2, h / 2), DegreeToRadian(angle));
  51.             return new Point[3] { first, RotatePoint(first, new Point(w / 2, h / 2), DegreeToRadian(120)), RotatePoint(first, new Point(w / 2, h / 2), DegreeToRadian(240)) };
  52.         }
  53.  
  54.         public static Bitmap ColorTriangle(int angle, int size, int multisampling)
  55.         {
  56.             size = size * multisampling;
  57.             using (Bitmap b = new Bitmap(size, size))
  58.             {
  59.                 using (Graphics g = Graphics.FromImage(b))
  60.                 {
  61.                     GraphicsPath gp = new GraphicsPath();
  62.                    
  63.                     Point origin = new Point(size / 2, size / 2);
  64.                     PointF first = new PointF(size, size / 2.0f);
  65.                     gp.AddPolygon(new PointF[3] { first, RotatePoint(first, origin, DegreeToRadian(120)), RotatePoint(first, origin, DegreeToRadian(240)) });
  66.  
  67.                     using (PathGradientBrush pgb = new PathGradientBrush(gp))
  68.                     {
  69.                         pgb.SurroundColors = new Color[] { Color.Black, Color.White };
  70.                         pgb.CenterPoint = RotatePoint(first, origin, DegreeToRadian(120));
  71.                         pgb.CenterColor = Color.White;//ColorManager.HSVtoRGB((short)angle, (byte)100, (byte)100, (byte)255);
  72.                         g.FillPath(pgb, gp);
  73.  
  74.                     }
  75.                     return (Bitmap)b.Clone();// ResizeImage(rotateImage(b, (float)angle - 120), size / multisampling);
  76.                    
  77.                 }
  78.             }
  79.         }
  80.  
  81.         static PointF CenterPolygon(PointF[] points)
  82.         {
  83.             PointF added = new PointF();
  84.  
  85.             foreach (var p in points)
  86.                 added = new PointF(added.X + p.X, added.Y + p.Y);
  87.  
  88.             added = new PointF(added.X / points.Length, added.Y / points.Length);
  89.             return added;
  90.         }
  91.  
  92.         private void pictureBox1_Paint(object sender, PaintEventArgs e)
  93.         {
  94.             var triangle = Triangle(pictureBox1.Width, pictureBox1.Height, 6);
  95.  
  96.             e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  97.  
  98.             e.Graphics.DrawImage(ColorTriangle(0, pictureBox1.Width / 2, 2), new Point(0, 0));
  99.  
  100.             Point origin = new Point(pictureBox1.Width / 2, pictureBox1.Width / 2);
  101.             PointF first = new PointF(pictureBox1.Width, pictureBox1.Width / 2.0f);
  102.             var polygon = new PointF[3] { first, RotatePoint(first, origin, DegreeToRadian(120)), RotatePoint(first, origin, DegreeToRadian(240)) };
  103.  
  104.             e.Graphics.DrawEllipse(Pens.Red, new RectangleF(PointF.Subtract(_clickPosition, new SizeF(2, 2)), new SizeF(4, 4)));
  105.  
  106.             int width = (int)(polygon[0].X - polygon[1].X);
  107.             int height = (int)(polygon[1].Y - polygon[2].Y);
  108.  
  109.             Point offsetFromPoint = new Point((int)polygon[0].X - _clickPosition.X, (int)polygon[0].Y - _clickPosition.Y);
  110.  
  111.             int maxY = (int)(((polygon[0].X - _clickPosition.X) / (float)width) * (float)height);
  112.             int highestY = (int)(((polygon[0].X - polygon[1].X) / (float)width) * (float)height);
  113.  
  114.             Text = highestY + " : " + maxY;
  115.  
  116.             var p = Pens.Green;
  117.             if (offsetFromPoint.Y > maxY - (maxY / 2) ||
  118.                 offsetFromPoint.Y < -(maxY - (maxY / 2)) ||
  119.                 offsetFromPoint.X > width)
  120.                 p = Pens.Red;
  121.  
  122.             Point clippedPosition = _clickPosition;
  123.  
  124.             if (offsetFromPoint.X > width)
  125.             {
  126.                 clippedPosition.X = (int)polygon[1].X;
  127.                 maxY = highestY;
  128.             }
  129.  
  130.             if (offsetFromPoint.Y > maxY - (maxY / 2))
  131.                 clippedPosition.Y = (int)(polygon[0].Y - (maxY - (maxY / 2)));
  132.             else if (offsetFromPoint.Y < -(maxY - (maxY / 2)))
  133.                 clippedPosition.Y = (int)(polygon[0].Y + (maxY - (maxY / 2)));
  134.  
  135.             e.Graphics.DrawEllipse(Pens.Red, new RectangleF(PointF.Subtract(clippedPosition, new SizeF(2, 2)), new SizeF(4, 4)));
  136.  
  137.             e.Graphics.DrawLine(p, polygon[0].X, _clickPosition.Y, polygon[0].X - width, _clickPosition.Y);
  138.             e.Graphics.DrawLine(p, _clickPosition.X, polygon[0].Y + (height / 2), _clickPosition.X, polygon[0].Y - (height / 2));
  139.             e.Graphics.DrawLine(p, _clickPosition.X, polygon[0].Y + (height / 2), _clickPosition.X, polygon[0].Y - (height / 2));
  140.         }
  141.     }
  142. }
Add Comment
Please, Sign In to add comment