public Line(namPoint start, namPoint end) { this.Start = start; this.End = end; this.Length = Math.Sqrt(((this.End.X - this.Start.X) * (this.End.X - this.Start.X)) + ((this.End.Y - this.Start.Y) * (this.End.Y - this.Start.Y))); }
public namBox(int size, namPoint center) { this.Center = center; this.size = size; Line north = new Line(new namPoint(center.X - size, center.Y - size), // NW Corner new namPoint(center.X + size, center.Y - size)); // NE Corner Line west = new Line(new namPoint(center.X - size, center.Y - size), // NW Corner new namPoint(center.X - size, center.Y + size)); // SW Corner Line south = new Line(new namPoint(center.X - size, center.Y + size), // SW Corner new namPoint(center.X + size, center.Y + size)); // SE Corner Line east = new Line(new namPoint(center.X + size, center.Y - size), // NE Corner new namPoint(center.X + size, center.Y + size)); // SE Corner this.Side = new Line[8]; // Directions enum ranges from 0-7 this.Side[(int)namDirections.North] = north; this.Side[(int)namDirections.West] = west; this.Side[(int)namDirections.South] = south; this.Side[(int)namDirections.East] = east; }
public namDirections GetDirectionForNext(namVect curVec, namPoint curLoc) { namDirections result = namDirections.Null; if (curVec.Length < (this.size * 3) && curVec.Length > 0) { int offset = Convert.ToInt32(Math.Floor((this.size * 3) / curVec.Length)); curVec.MultiplyBy(offset); // make longer to ensure it will intersect side } Line north = this.Side[(int)namDirections.North]; Line west = this.Side[(int)namDirections.West]; Line south = this.Side[(int)namDirections.South]; Line east = this.Side[(int)namDirections.East]; Line test = curVec.toLine(curLoc); if (this.DoLinesIntersect(north, test)) { namPoint intersect = this.GetLinesIntersect(north, test); if (intersect == north.Start) { result = namDirections.NorthWest; } else if (intersect == north.End) { result = namDirections.NorthEast; } else { result = namDirections.North; } } else if (this.DoLinesIntersect(west, test)) { namPoint intersect = this.GetLinesIntersect(west, test); if (intersect == west.Start) { result = namDirections.NorthWest; } else if (intersect == west.End) { result = namDirections.SouthWest; } else { result = namDirections.West; } } else if (this.DoLinesIntersect(south, test)) { namPoint intersect = this.GetLinesIntersect(west, test); if (intersect == south.Start) { result = namDirections.SouthWest; } else if (intersect == south.End) { result = namDirections.SouthEast; } else { result = namDirections.South; } } else if (this.DoLinesIntersect(east, test)) { namPoint intersect = this.GetLinesIntersect(west, test); if (intersect == east.Start) { result = namDirections.NorthEast; } else if (intersect == east.End) { result = namDirections.SouthEast; } else { result = namDirections.East; } } return result; }
private namPoint GetLinesIntersect(Line line1, Line line2) { namPoint results = new namPoint(-1, -1); double ua = (line2.End.X - line2.Start.X) * (line1.Start.Y - line2.Start.Y) - (line2.End.Y - line2.Start.Y) * (line1.Start.X - line2.Start.X); double ub = (line1.End.X - line1.Start.X) * (line1.Start.Y - line2.Start.Y) - (line1.End.Y - line1.Start.Y) * (line1.Start.X - line2.Start.X); double denominator = (line2.End.Y - line2.Start.Y) * (line1.End.X - line1.Start.X) - (line2.End.X - line2.Start.X) * (line1.End.Y - line1.Start.Y); if (Math.Abs(denominator) <= 0.00001f) { if (Math.Abs(ua) <= 0.00001f && Math.Abs(ub) <= 0.00001f) { results.X = (line1.Start.X + line1.End.X) / 2; results.Y = (line1.Start.Y + line1.End.Y) / 2; } } else { ua /= denominator; ub /= denominator; if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) { results.X = line1.Start.X + ua * (line1.End.X - line1.Start.X); results.Y = line1.Start.Y + ua * (line1.End.Y - line1.Start.Y); } } return results; }
public bool isPointInBox(namPoint p) { bool results = true; if (p != this.Center) { Line test = new Line(this.Center, p); if (test.Length >= this.size) { //for (int i = 0; i < 4; i++) //{ // if (this.DoLinesIntersect(test, this.Side[i])) // { // results = false; // } //} results = false; } } else { results = true; } return results; }
public namVect(namPoint start, namPoint end) { this.X = end.X - start.X; this.Y = end.Y - start.Y; this.Init(); }
private namDirections FindDirection(namPoint p) { namDirections result = namDirections.Null; if (p.X == 0) { // X is zero, moving either North or South if (p.Y == 0) { // Y is zero, not moving at all! result = namDirections.Null; } else if (p.Y > 0) { // Y is positive, moving to the South result = namDirections.South; } else { // Y is negative, moving to the North result = namDirections.North; } } else if (p.X > 0) { // X is positive, moving to the East if (p.Y == 0) { // Y is zero, moving East result = namDirections.East; } else if (p.Y > 0) { // Y is positive, moving to the SouthEast result = namDirections.SouthEast; } else { // Y is negative, moving to the NorthEast result = namDirections.NorthEast; } } else { // X is negative, moving to the West if (p.Y == 0) { // Y is zero, moving West result = namDirections.West; } else if (p.Y > 0) { // Y is positive, moving to the SouthWest result = namDirections.SouthWest; } else { // Y is negative, moving to the NorthWest result = namDirections.NorthWest; } } return result; }
public Line toLine(namPoint start) { namPoint end = new namPoint(start.X + this.X, start.Y + this.Y); return new Line(start, end); }