123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SketchAssistantWPF
- {
-
-
-
- public static class GeometryCalculator
- {
-
-
-
-
-
-
-
- public static HashSet<Point> FilledCircleAlgorithm(Point center, int radius)
- {
- HashSet<Point> returnSet = new HashSet<Point> { center };
-
- for (int x = 0; x < radius; x++)
- {
- for (int y = 0; y < radius; y++)
- {
-
- if ((x * x + y * y - radius * radius) <= 0)
- {
- returnSet.Add(new Point(center.X + x, center.Y + y));
- returnSet.Add(new Point(center.X - x, center.Y + y));
- returnSet.Add(new Point(center.X + x, center.Y - y));
- returnSet.Add(new Point(center.X - x, center.Y - y));
- }
- }
- }
- return returnSet;
- }
-
-
-
-
-
-
-
-
- public static List<Point> BresenhamLineAlgorithm(Point p0, Point p1)
- {
- int p1x = (int)p1.X;
- int p1y = (int)p1.Y;
- int p0x = (int)p0.X;
- int p0y = (int)p0.Y;
- int deltaX = p1x - p0x;
- int deltaY = p1y - p0y;
- List<Point> returnList;
- if (Math.Abs(deltaY) < Math.Abs(deltaX))
- {
- if (p0.X > p1.X)
- {
- returnList = GetLineLow(p1x, p1y, p0x, p0y);
- returnList.Reverse();
- }
- else
- {
- returnList = GetLineLow(p0x, p0y, p1x, p1y);
- }
- }
- else
- {
- if (p0.Y > p1.Y)
- {
- returnList = GetLineHigh(p1x, p1y, p0x, p0y);
- returnList.Reverse();
- }
- else
- {
- returnList = GetLineHigh(p0x, p0y, p1x, p1y);
- }
- }
- return returnList;
- }
-
-
-
-
-
-
-
-
-
-
- private static List<Point> GetLineLow(int x0, int y0, int x1, int y1)
- {
- List<Point> returnList = new List<Point>();
- int dx = x1 - x0;
- int dy = y1 - y0;
- int yi = 1;
- if (dy < 0)
- {
- yi = -1;
- dy = -dy;
- }
- int D = 2 * dy - dx;
- int y = y0;
- for (int x = x0; x <= x1; x++)
- {
- returnList.Add(new Point(x, y));
- if (D > 0)
- {
- y = y + yi;
- D = D - 2 * dx;
- }
- D = D + 2 * dy;
- }
- return returnList;
- }
-
-
-
-
-
-
-
-
-
-
- private static List<Point> GetLineHigh(int x0, int y0, int x1, int y1)
- {
- List<Point> returnList = new List<Point>();
- int dx = x1 - x0;
- int dy = y1 - y0;
- int xi = 1;
- if (dx < 0)
- {
- xi = -1;
- dx = -dx;
- }
- int D = 2 * dx - dy;
- int x = x0;
- for (int y = y0; y <= y1; y++)
- {
- returnList.Add(new Point(x, y));
- if (D > 0)
- {
- x = x + xi;
- D = D - 2 * dy;
- }
- D = D + 2 * dx;
- }
- return returnList;
- }
- }
- }
|