public Triad FindPointSet(Triad triangleToFind) { Triad foundShape = null; if (VerifyUniquePts(triangleToFind)) { foreach (Triad triangle in _grid) { bool isPtAPresent = TriangleContainsPt(triangle, triangleToFind.StartingPt); bool isPtBPresent = TriangleContainsPt(triangle, triangleToFind.MidPt); bool isPtCPresent = TriangleContainsPt(triangle, triangleToFind.EndingPt); if (isPtAPresent && isPtBPresent && isPtCPresent) { foundShape = new Triad() { StartingPt = triangle.StartingPt, MidPt = triangle.MidPt, EndingPt = triangle.EndingPt, Label = triangle.Label }; break; } } } return(foundShape); }
private void Load(int shapeWidth = 10, int shapeHeight = 10) { for (int curRow = 0; curRow < _maxRows; curRow++) { string rowLabel = RowLabelBuilder(curRow + 1); int seqNo = 1; for (int curCol = 0; curCol < _maxCols; curCol++) { int baseX = curCol * shapeWidth; int baseY = curRow * shapeHeight; string firstColLabel = seqNo.ToString(); seqNo++; Triad shape1 = new Triad(); shape1.StartingPt = GetStartPointCoords(baseX, baseY, curRow, curCol, true); shape1.MidPt = GetMidPointCoords(baseX, baseY, curRow, curCol, true); shape1.EndingPt = GetEndingPointCoords(baseX, baseY, curRow, curCol, true); shape1.Label = rowLabel + firstColLabel; _grid.Add(shape1); string secondColLabel = (seqNo).ToString(); seqNo++; Triad shape2 = new Triad(); shape2.StartingPt = GetStartPointCoords(baseX, baseY, curRow, curCol, false); shape2.MidPt = GetMidPointCoords(baseX, baseY, curRow, curCol, false); shape2.EndingPt = GetEndingPointCoords(baseX, baseY, curRow, curCol, false); shape2.Label = rowLabel + secondColLabel; _grid.Add(shape2); } } }
private static void DumpTriangle(Triad triangle) { Console.WriteLine("Triangle: " + triangle.Label + " (" + triangle.StartingPt.x.ToString() + "," + triangle.StartingPt.y.ToString() + ")," + " (" + triangle.MidPt.x.ToString() + "," + triangle.MidPt.y.ToString() + ")," + " (" + triangle.EndingPt.x.ToString() + "," + triangle.EndingPt.y.ToString() + ")"); }
public void Lookup(Triad triangle) { Console.WriteLine("\n\nLooking up triangle: " + " (" + triangle.StartingPt.x.ToString() + "," + triangle.StartingPt.y.ToString() + ")," + " (" + triangle.MidPt.x.ToString() + "," + triangle.MidPt.y.ToString() + ")," + " (" + triangle.EndingPt.x.ToString() + "," + triangle.EndingPt.y.ToString() + ")"); if (!VerifyUniquePts(triangle)) { Console.WriteLine("Warning: the points entered are not three unique set of coordinates (so no search will be done)"); } else { Triad foundShape = FindPointSet(triangle); if (foundShape == null) { Console.WriteLine("Triangle not found!"); } else { Console.WriteLine("Triangle found: " + foundShape.Label); } } }
private static bool VerifyUniquePts(Triad triangle) { bool dupe1 = CoordsMatch(triangle.StartingPt, triangle.MidPt); bool dupe2 = CoordsMatch(triangle.StartingPt, triangle.EndingPt); bool dupe3 = CoordsMatch(triangle.MidPt, triangle.EndingPt); return(!dupe1 && !dupe2 && !dupe3); }
static void Main(string[] args) { Triad queryPts = TriangleTestUtils.ProcessArgs(args); GridBuilder gb = new GridBuilder(6, 6); Console.WriteLine("\nTriangle Coord Building Underway..."); gb.GridDump(); if (queryPts != null) { gb.Lookup(queryPts); } Console.WriteLine("\nTriangle Building/Lookup Complete"); }
// dig out 3 pairs of points which define a triangle that is to be looked up public static Triad ProcessArgs(string[] args) { bool ok = false; Triad tmpShape = new Triad(); int xCoord, yCoord, i = 0; if (args.Length == 3) { if ((args[i] != null) && (args[i].Length >= 3) && (args[i].IndexOf(",") > 0)) { if (ExtractPts(args[i], out xCoord, out yCoord)) { i++; tmpShape.StartingPt = new Point() { x = xCoord, y = yCoord }; if ((args[i] != null) && (args[i].Length >= 3) && (args[i].IndexOf(",") > 0)) { if (ExtractPts(args[i], out xCoord, out yCoord)) { i++; tmpShape.MidPt = new Point() { x = xCoord, y = yCoord }; if ((args[i] != null) && (args[i].Length >= 3) && (args[i].IndexOf(",") > 0)) { if (ExtractPts(args[i], out xCoord, out yCoord)) { tmpShape.EndingPt = new Point() { x = xCoord, y = yCoord }; tmpShape.Label = ""; ok = true; } } } } } } } return(ok ? tmpShape : null); }
private static bool TriangleContainsPt(Triad triangle, Point pt) { return(CoordsMatch(triangle.StartingPt, pt) || CoordsMatch(triangle.MidPt, pt) || CoordsMatch(triangle.EndingPt, pt)); }