Question

I have a project with a graphic object called GraphicsLine. What it does is simply draw line on every mousedown and stop on every mouseup, nothing complicated. It stores coordinates for the start and the end of the line (x,y). Now what I want to know is whenever a shape is created. For example, you create 4 lines that forms a square, I want to be able to run an algorithm that can tell me that there is a square in the drawing.

Note that the shape can be anything that is "closed". Not only square, rectangle or triangle.

The goal of this is to calculate the area of the created shapes.

Is there something that already exists for doing this? I've been struggling to find something that could fit my needs.

EDIT 1:

I added some additionnal information :

Lines are either "cliped" to another line start or end point or they are not. There is no close closure, it is on the same point or not closed at all. 1 line can be used in multiple shapes.

EDIT 2 :

So basically, I want something that can give me an array of "GraphicsLine" that forms a shape. So if we have 6 lines in the drawing but 4 of them forms a square, I want something that returns those 4 lines so I can create another object from it.

Thanks in advance.

Was it helpful?

Solution 3

I know it is 2 years + later, but I found a way with a recursive function to know when the shapes are "closed". You start from any points of the last drawed line, then you check if the other point is connected to another line. You do this until you reach the starting point. I save all the lines into another class called Polygon. This way, they keep all the lines with the start and finish that forms the polygon. Then to calculate the area, I do as Eduardo Cobuci said in his answer.

Hope this helps.

OTHER TIPS

Please check this question How do I calculate the area of a 2d polygon? it is probably what you need, you just have to port it to C# :)

Edit: from @chmike answer:

Where x and y are the arrays of coordinates

var x = [10,10,20,20];
var y = [10,20,20,10];

var n = x.Length;
x[n] = x[0];
x[n+1] = x[1];
y[n] = y[0];
y[n+1] = y[1];

// compute area
int area = 0;
for(var i = 1; i <= n; ++i ) {
  area += x[i]*( y[i+1] - y[i-1] );
}

Console.Write(area /= 2);

Take a look at this tutorial of AForge.NET Shape Checker

If your GraphicsLine object is drawing on a PictureBox or if you can convert that object to a bitmap, you can then run the code below.

You can easily try detection of quadrilaterals, this code here will actually highlight the detected objects, you do change the loop and make it do whatever you want instead:

// if you are using a PictureBox to draw the shapes, then convert it to a bitmap
Bitmap bitmap = new Bitmap(pictureBox1.Image)

// locate objects using blob counter
BlobCounter blobCounter = new BlobCounter( );

blobCounter.ProcessImage( bitmap );

Blob[] blobs = blobCounter.GetObjectsInformation( );

// create Graphics object to draw on the image and a pen
Graphics g = Graphics.FromImage( bitmap );

Pen bluePen = new Pen( Color.Blue, 2 );

// check each object and draw circle around objects, which are recognized as circles
for ( int i = 0, n = blobs.Length; i < n; i++ )
{
    List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints( blobs[i] );
    List<IntPoint> corners = PointsCloud.FindQuadrilateralCorners( edgePoints );

    g.DrawPolygon( bluePen, ToPointsArray( corners ) );    
}

bluePen.Dispose( );
g.Dispose( );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top