33using System.Collections.Generic;
44 bool this[
int x,
int y] {
get;}
51 if (
bitmap ==
null) {
throw new ArgumentNullException(
"bitmap"); }
56 get {
return bitmap.GetLength(0); }
60 get {
return bitmap.GetLength(1); }
62 public bool this[
int x,
int y]
67 x >= 0 && x <
Width &&
92 if (points ==
null) {
throw new ArgumentNullException(
"vectors"); }
93 if (points.Count == 0) {
throw new ArgumentOutOfRangeException(
"points"); }
96 for (
int index = 1; index < points.Count; ++index)
98 Point2D current = points[index];
103 else if (current.
X <
xMin)
112 for (
int index = 0; index <
scans.Length; ++index)
114 scans[index] =
new List<int>();
119 for (
int index = 0; index < points.Count; ++index)
122 int scanIndex = point.
X -
xMin;
123 scans[scanIndex].Add(point.
Y);
129 for (
int index = 0; index <
scans.Length; ++index)
137 List<int> scan =
scans[x];
138 List<int> newScan =
new List<int>();
140 for (
int index = 0; index < scan.Count; ++index)
147 bool value = bitmap[x +
xMin, y + 1];
163 int scanIndex = point.
X -
xMin;
164 if (scanIndex < 0 || scanIndex >=
scans.Length)
169 List<int> scan =
scans[scanIndex];
170 for (
int index = 1; index < scan.Count; index += 2)
172 if (point.
Y >= scan[index - 1] &&
173 point.
Y <= scan[index])
200 List<BitMapSkipper> skippers =
new List<BitMapSkipper>();
201 List<Vector2D[]> result =
new List<Vector2D[]>();
206 skippers.Add(skipper);
207 result.Add(
Reduce(points));
209 return result.ToArray();
215 List<Point2D> result =
new List<Point2D>();
220 last = result[result.Count - 1];
221 }
while (current != first);
222 if (result.Count < 3) {
throw new ArgumentException(
"The image has an area with less then 3 pixels (possibly an artifact).",
"bitmap"); }
227 for (
int x = bitmap.
Width - 1; x > -1; --x)
229 for (
int y = 0; y < bitmap.
Height; ++y)
237 throw new ArgumentException(
"TODO",
"bitmap");
239 private static IEnumerable<Point2D>
GetFirsts(
IBitmap bitmap, List<BitMapSkipper> skippers)
241 for (
int x = bitmap.
Width - 1; x > -1; --x)
243 for (
int y = 0; y < bitmap.
Height; ++y)
247 bool contains =
false;
249 for (
int index = 0; index < skippers.Count; ++index)
252 if (skippers[index].TryGetSkip(result, out nextY))
271 for (
int index = 0; index <
bitmapPoints.Length; ++index)
280 for (
int index = 0; index <
bitmapPoints.Length; ++index)
286 if (point.X >= 0 && point.X < bitmap.
Width &&
287 point.Y >= 0 && point.Y < bitmap.
Height &&
288 bitmap[point.X, point.Y])
293 throw new ArgumentException(
"The image has an area with less then 3 pixels (possibly an artifact).",
"bitmap");
297 List<Vector2D> result =
new List<Vector2D>(list.Count);
298 Point2D p1 = list[list.Count - 2];
299 Point2D p2 = list[list.Count - 1];
301 for (
int index = 0; index < list.Count; ++index, p2 = p3)
303 if (index == list.Count - 1)
305 if (result.Count == 0) {
throw new ArgumentException(
"Bad Polygon"); }
306 p3.
X = (int)result[0].
X;
307 p3.
Y = (int)result[0].
Y;
309 else { p3 = list[index]; }
310 if (!
IsInLine(ref p1, ref p2, ref p3))
316 return result.ToArray();
320 int slope1 = (p1.Y - p2.Y);
321 int slope2 = (p2.Y - p3.Y);
322 return 0 == slope1 && 0 == slope2 ||
323 ((p1.X - p2.X) / (
Scalar)slope1) == ((p2.X - p3.X) / (
Scalar)slope2);
ArrayBitmap(bool[,] bitmap)
bool TryGetSkip(Point2D point, out int nextY)
void FormatScans(IBitmap bitmap)
BitMapSkipper(IBitmap bitmap, List< Point2D > points)
void FormatScan(IBitmap bitmap, int x)
void FillScans(List< Point2D > points)
void FromVectors(List< Point2D > points)
static Point2D GetFirst(IBitmap bitmap)
static List< Point2D > CreateFromBitmap(IBitmap bitmap, Point2D first)
static Vector2D[] CreateFromBitmap(IBitmap bitmap)
static Vector2D[][] CreateManyFromBitmap(IBitmap bitmap)
static IEnumerable< Point2D > GetFirsts(IBitmap bitmap, List< BitMapSkipper > skippers)
static Point2D GetNextVertex(IBitmap bitmap, Point2D current, Point2D last)
static Vector2D[] Reduce(List< Point2D > list)
static bool IsInLine(ref Point2D p1, ref Point2D p2, ref Point2D p3)
static readonly Point2D[] bitmapPoints
This is the Vector Class.
static Point2D Add(Point2D left, Point2D right)
Adds 2 Vectors2Ds.
override bool Equals(object obj)
Compares this Vector to another object. This should be done because the equality operators (==,...
int Y
This is the Y value. (Usually represents a vertical position or direction.)
int X
This is the X value. (Usually represents a horizontal position or direction.)
This is the Vector Class.