123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- namespace SketchAssistant
- {
- public class Line
- {
-
-
-
- private List<Point> linePoints;
-
-
-
- private int identifier;
-
-
-
- private bool isTemporary;
-
-
-
-
-
- public Line(List<Point> points)
- {
- linePoints = new List<Point>(points);
- isTemporary = true;
- }
-
-
-
-
-
-
- public Line(List<Point> points, int id)
- {
- linePoints = new List<Point>(points);
- identifier = id;
- CleanPoints();
- isTemporary = false;
- }
- public Point GetStartPoint()
- {
- return linePoints.First();
- }
- public Point GetEndPoint()
- {
- return linePoints.Last();
- }
- public List<Point> GetPoints()
- {
- return linePoints;
- }
- public int GetID()
- {
- return identifier;
- }
-
-
-
-
-
-
- public Graphics DrawLine(Graphics canvas)
- {
- for(int i = 0; i < linePoints.Count - 1 ; i++)
- {
- canvas.DrawLine(Pens.Black, linePoints[i], linePoints[i + 1]);
- }
-
- if(linePoints.Count == 1){ canvas.FillRectangle(Brushes.Black, linePoints[0].X, linePoints[0].Y, 1, 1); }
- return canvas;
- }
-
-
-
-
-
- public void PopulateMatrixes(bool[,] boolMatrix, HashSet<int>[,] listMatrix)
- {
- if(!isTemporary)
- {
- foreach (Point currPoint in linePoints)
- {
- if (currPoint.X >= 0 && currPoint.Y >= 0 &&
- currPoint.X < boolMatrix.GetLength(0) && currPoint.Y < boolMatrix.GetLength(1))
- {
- boolMatrix[currPoint.X, currPoint.Y] = true;
- if (listMatrix[currPoint.X, currPoint.Y] == null)
- {
- listMatrix[currPoint.X, currPoint.Y] = new HashSet<int>();
- }
- listMatrix[currPoint.X, currPoint.Y].Add(identifier);
- }
- }
- }
- }
-
-
-
- private void CleanPoints()
- {
- if (linePoints.Count > 1)
- {
- List<Point> newList = new List<Point>();
- List<Point> tempList = new List<Point>();
-
-
- int nullValue = linePoints[0].X + 1;
-
- for (int i = 0; i < linePoints.Count - 1; i++)
- {
- nullValue += linePoints[i + 1].X;
- List<Point> partialList = GeometryCalculator.BresenhamLineAlgorithm(linePoints[i], linePoints[i + 1]);
- tempList.AddRange(partialList);
- }
- Point nullPoint = new Point(nullValue, 0);
-
- for (int i = 1; i < tempList.Count; i++)
- {
- if ((tempList[i].X == tempList[i - 1].X) && (tempList[i].Y == tempList[i - 1].Y))
- {
- tempList[i - 1] = nullPoint;
- }
- }
-
- foreach (Point tempPoint in tempList)
- {
- if (tempPoint.X != nullValue)
- {
- newList.Add(tempPoint);
- }
- }
- linePoints = new List<Point>(newList);
- }
- }
- }
- }
|