public Line(sPoint start, sPoint 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 sBox(int size, sPoint center) { this.Center = center; this.size = size; Line north = new Line(new sPoint(center.X - size, center.Y - size), // NW Corner new sPoint(center.X + size, center.Y - size)); // NE Corner Line west = new Line(new sPoint(center.X - size, center.Y - size), // NW Corner new sPoint(center.X - size, center.Y + size)); // SW Corner Line south = new Line(new sPoint(center.X - size, center.Y + size), // SW Corner new sPoint(center.X + size, center.Y + size)); // SE Corner Line east = new Line(new sPoint(center.X + size, center.Y - size), // NE Corner new sPoint(center.X + size, center.Y + size)); // SE Corner this.Side = new Line[8]; // Directions enum ranges from 0-7 this.Side[(int)sDirections.North] = north; this.Side[(int)sDirections.West] = west; this.Side[(int)sDirections.South] = south; this.Side[(int)sDirections.East] = east; }
public sDirections GetDirectionForNext(sVect curVec, sPoint curLoc) { sDirections result = sDirections.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)sDirections.North]; Line west = this.Side[(int)sDirections.West]; Line south = this.Side[(int)sDirections.South]; Line east = this.Side[(int)sDirections.East]; Line test = curVec.toLine(curLoc); if (this.DoLinesIntersect(north, test)) { sPoint intersect = this.GetLinesIntersect(north, test); if (intersect == north.Start) { result = sDirections.NorthWest; } else if (intersect == north.End) { result = sDirections.NorthEast; } else { result = sDirections.North; } } else if (this.DoLinesIntersect(west, test)) { sPoint intersect = this.GetLinesIntersect(west, test); if (intersect == west.Start) { result = sDirections.NorthWest; } else if (intersect == west.End) { result = sDirections.SouthWest; } else { result = sDirections.West; } } else if (this.DoLinesIntersect(south, test)) { sPoint intersect = this.GetLinesIntersect(west, test); if (intersect == south.Start) { result = sDirections.SouthWest; } else if (intersect == south.End) { result = sDirections.SouthEast; } else { result = sDirections.South; } } else if (this.DoLinesIntersect(east, test)) { sPoint intersect = this.GetLinesIntersect(west, test); if (intersect == east.Start) { result = sDirections.NorthEast; } else if (intersect == east.End) { result = sDirections.SouthEast; } else { result = sDirections.East; } } return result; }
private sPoint GetLinesIntersect(Line line1, Line line2) { sPoint results = new sPoint(-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(sPoint 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 sVect(sPoint start, sPoint end) { this.X = end.X - start.X; this.Y = end.Y - start.Y; this.Init(); }
private sDirections FindDirection(sPoint p) { sDirections result = sDirections.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 = sDirections.Null; } else if (p.Y > 0) { // Y is positive, moving to the South result = sDirections.South; } else { // Y is negative, moving to the North result = sDirections.North; } } else if (p.X > 0) { // X is positive, moving to the East if (p.Y == 0) { // Y is zero, moving East result = sDirections.East; } else if (p.Y > 0) { // Y is positive, moving to the SouthEast result = sDirections.SouthEast; } else { // Y is negative, moving to the NorthEast result = sDirections.NorthEast; } } else { // X is negative, moving to the West if (p.Y == 0) { // Y is zero, moving West result = sDirections.West; } else if (p.Y > 0) { // Y is positive, moving to the SouthWest result = sDirections.SouthWest; } else { // Y is negative, moving to the NorthWest result = sDirections.NorthWest; } } return result; }
public Line toLine(sPoint start) { sPoint end = new sPoint(start.X + this.X, start.Y + this.Y); return new Line(start, end); }