-1

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.

Crushermike
  • 205
  • 3
  • 20
  • You will have to define your rules clearer, I think. What about line portions that cross or are longer than needed to close? Also you will need a magnet radius to detect "close closures".. Can more than two lines connect? etc.. – TaW Mar 19 '14 at 18:01
  • Ok, Lines can't be longer. I have code that "clips" them to the start or the end point of the other line when they are close. There is no close closures since it is closed or not closed. If not closed, then it doesn't form a shape. Yes more than 2 lines can be connected togheter. So you can have 2 squares that forms a bigger rectangle for example. – Crushermike Mar 19 '14 at 18:07

3 Answers3

1

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);
Community
  • 1
  • 1
Eduardo Cobuci
  • 5,621
  • 4
  • 25
  • 27
  • 404 not found when I click on the link inside the answer of Darius Bacon. :( – Crushermike Mar 19 '14 at 18:15
  • This gives me only the area while what I need is the shape. Because after I know that I have a closed shape, I can create a new object for the shape and then use the code to calculate an area (which I already had and look like what you just gave me). – Crushermike Mar 19 '14 at 18:45
0

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( );
lucidgold
  • 4,432
  • 5
  • 31
  • 51
  • Sorry for the late answer, but I checked that object with is very amazing, but not what I need. It wasn't in picture box or bitmap. But thanks anyways. – Crushermike Mar 21 '14 at 18:37
0

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.

Community
  • 1
  • 1
Crushermike
  • 205
  • 3
  • 20