Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing.Drawing2D;
- namespace TriangleClipping
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- pictureBox1.Resize += new EventHandler(pictureBox1_Resize);
- }
- void pictureBox1_Resize(object sender, EventArgs e)
- {
- pictureBox1.Invalidate();
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- _clickPosition = PointToClient(Cursor.Position);
- pictureBox1.Invalidate();
- }
- Point _clickPosition = new Point(128 / 2, 128 / 2);
- public static Point RotatePoint(PointF p, PointF p2, double angle)
- { //angle in radians.
- 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));
- }
- public static double DegreeToRadian(double angle)
- {
- return Math.PI * angle / 180.0;
- }
- public static double RadianToDegree(double angle)
- {
- return angle * 180.0 / Math.PI;
- }
- public static Point[] Triangle(int w, int h, int angle)
- {
- Point first = RotatePoint(new Point(w, h), new Point(w / 2, h / 2), DegreeToRadian(angle));
- 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)) };
- }
- public static Bitmap ColorTriangle(int angle, int size, int multisampling)
- {
- size = size * multisampling;
- using (Bitmap b = new Bitmap(size, size))
- {
- using (Graphics g = Graphics.FromImage(b))
- {
- GraphicsPath gp = new GraphicsPath();
- Point origin = new Point(size / 2, size / 2);
- PointF first = new PointF(size, size / 2.0f);
- gp.AddPolygon(new PointF[3] { first, RotatePoint(first, origin, DegreeToRadian(120)), RotatePoint(first, origin, DegreeToRadian(240)) });
- using (PathGradientBrush pgb = new PathGradientBrush(gp))
- {
- pgb.SurroundColors = new Color[] { Color.Black, Color.White };
- pgb.CenterPoint = RotatePoint(first, origin, DegreeToRadian(120));
- pgb.CenterColor = Color.White;//ColorManager.HSVtoRGB((short)angle, (byte)100, (byte)100, (byte)255);
- g.FillPath(pgb, gp);
- }
- return (Bitmap)b.Clone();// ResizeImage(rotateImage(b, (float)angle - 120), size / multisampling);
- }
- }
- }
- static PointF CenterPolygon(PointF[] points)
- {
- PointF added = new PointF();
- foreach (var p in points)
- added = new PointF(added.X + p.X, added.Y + p.Y);
- added = new PointF(added.X / points.Length, added.Y / points.Length);
- return added;
- }
- private void pictureBox1_Paint(object sender, PaintEventArgs e)
- {
- var triangle = Triangle(pictureBox1.Width, pictureBox1.Height, 6);
- e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
- e.Graphics.DrawImage(ColorTriangle(0, pictureBox1.Width / 2, 2), new Point(0, 0));
- Point origin = new Point(pictureBox1.Width / 2, pictureBox1.Width / 2);
- PointF first = new PointF(pictureBox1.Width, pictureBox1.Width / 2.0f);
- var polygon = new PointF[3] { first, RotatePoint(first, origin, DegreeToRadian(120)), RotatePoint(first, origin, DegreeToRadian(240)) };
- e.Graphics.DrawEllipse(Pens.Red, new RectangleF(PointF.Subtract(_clickPosition, new SizeF(2, 2)), new SizeF(4, 4)));
- int width = (int)(polygon[0].X - polygon[1].X);
- int height = (int)(polygon[1].Y - polygon[2].Y);
- Point offsetFromPoint = new Point((int)polygon[0].X - _clickPosition.X, (int)polygon[0].Y - _clickPosition.Y);
- int maxY = (int)(((polygon[0].X - _clickPosition.X) / (float)width) * (float)height);
- int highestY = (int)(((polygon[0].X - polygon[1].X) / (float)width) * (float)height);
- Text = highestY + " : " + maxY;
- var p = Pens.Green;
- if (offsetFromPoint.Y > maxY - (maxY / 2) ||
- offsetFromPoint.Y < -(maxY - (maxY / 2)) ||
- offsetFromPoint.X > width)
- p = Pens.Red;
- Point clippedPosition = _clickPosition;
- if (offsetFromPoint.X > width)
- {
- clippedPosition.X = (int)polygon[1].X;
- maxY = highestY;
- }
- if (offsetFromPoint.Y > maxY - (maxY / 2))
- clippedPosition.Y = (int)(polygon[0].Y - (maxY - (maxY / 2)));
- else if (offsetFromPoint.Y < -(maxY - (maxY / 2)))
- clippedPosition.Y = (int)(polygon[0].Y + (maxY - (maxY / 2)));
- e.Graphics.DrawEllipse(Pens.Red, new RectangleF(PointF.Subtract(clippedPosition, new SizeF(2, 2)), new SizeF(4, 4)));
- e.Graphics.DrawLine(p, polygon[0].X, _clickPosition.Y, polygon[0].X - width, _clickPosition.Y);
- e.Graphics.DrawLine(p, _clickPosition.X, polygon[0].Y + (height / 2), _clickPosition.X, polygon[0].Y - (height / 2));
- e.Graphics.DrawLine(p, _clickPosition.X, polygon[0].Y + (height / 2), _clickPosition.X, polygon[0].Y - (height / 2));
- }
- }
- }
Add Comment
Please, Sign In to add comment