using System;
using System.Collections;
namespace ConsoleApplication1
{
class Shape
{
public string Shapename;
public int x1, y1, x2, y2;
public bool IsInside(int x, int y)
{
if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
return true;
return false;
}
}
{
private const int xArr = 10, yArr = 10;
private const int xLen = 128, yLen = 128;
{
public int left, top, right, bottom;
public bool Search(int x, int y)
{
if (ShapeIndexs.Count > 0)
for (int n = (ShapeIndexs.Count - 1); n >= 0; n--)
{
Shape shape = ((Shape)Shapes[(int)ShapeIndexs[n]]);
if (shape.IsInside(x, y) == true)
{
Console.
WriteLine("({0}, {1}) Exist in Shape: {2}", x, y, shape.
Sha
pename); return true;
}
}
Console.
WriteLine("No shapes at ({0}, {1})", x, y
); return false;
}
}
{
for (int x = 0; x < xArr; x++)
for (int y = 0; y < yArr; y++)
{
Regions[x, y].left = x * xLen;
Regions[x, y].right = ((x + 1) * xLen) - 1;
Regions[x, y].top = y * yLen;
Regions[x, y].bottom = ((y + 1) * yLen) - 1;
}
}
public void GetShapes()
{
string sName = "";
Console.
WriteLine("nEnter Shapes... "); do
{
if (sName.Length > 0)
{
Shape shape = new Shape();
shape.Shapename = sName;
InsertShape(ref shape);
}
else
break;
}
while (sName.Length > 0);
}
public bool InsertShape(ref Shape shape)
{
int x1 = shape.x1 / xLen;
int y1 = shape.y1 / yLen;
int x2 = shape.x2 / xLen;
int y2 = shape.y2 / yLen;
int ShapeIndex;
if (x2 < xArr && y2 < yArr)
{
ShapeIndex = Shapes.Add(shape);
for (int x = x1; x <= x2; x++)
for (int y = y1; y <= y2; y++)
{
Regions[x, y].ShapeIndexs.Add(ShapeIndex);
Console.
WriteLine("Shape inserted at Regions[{0}, {1}]", x, y
); }
return true;
}
return false;
}
public bool SearchShape(int xPos, int yPos)
{
int x = xPos / xLen;
int y = yPos / yLen;
if (x < xArr && y < yArr)
{
Console.
WriteLine("Searching in Regions[{0}, {1}]", x, y
); return Regions[x, y].Search(xPos, yPos);
}
return false;
}
}
class Program
{
static void Main(string[] args)
{
int x, y;
scr.GetShapes();
while (GetXY(out x, out y) == true)
scr.SearchShape(x, y);
}
static public bool GetXY(out int x, out int y)
{
Console.
WriteLine("nCoordinates... ");
if (x >= 0 && y >= 0)
return true;
return false;
}
}
}