# Code in C

Page 1 of 1

## 2 Replies - 261 Views - Last Post: 14 September 2012 - 03:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291897&amp;s=1f9bef98c0558b306d0214e84a96dd18&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dulmuniz

Reputation: 0
• Posts: 2
• Joined: 13-September 12

# Code in C

Posted 13 September 2012 - 07:27 PM

Hi I am trying to do a program in c and run it in Eclipse. The program is about calculating the area of polygons in a map and it property, but i am getting some error when import the Shape lib (SHP dump) into eclipse and also i need a code of how to calculate the area and property of polygons (name and coordinates. any one knows how to do this. thanks.

```#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <shapelip.h>
#include <shape.h>

#define MAXINTERSECTIONPOINTS 255

enum loopDir {\
kExterior,
kInterior,
kError
};

struct DPoint2d
{
DPoint2d()
{
x = y = 0.0;
};
DPoint2d(double x, double y)
{
this->x = x;
this->y = y;
};
double x,y;
};

struct IntersectPoint
{
IntersectPoint(void)
{
x = y = 0.0;
boundry_nmb = 0;
loopdirection = kError;
};
double x,y;
int boundry_nmb;
loopDir loopdirection;
};

loopDir LoopDirection(DPoint2d *vertices, int vertsize)
{
int i;
double sum = 0.0;
for(i=0;i<vertsize-1;i++)
{
sum += (vertices[i].x*vertices[i+1].y)-(vertices[i].y*vertices[i+1].x);
}

if(sum>0)
return kInterior;
else
return kExterior;
}

DPoint2d CreatePointInPoly(SHPObject *psShape, int quality)
{
int i, j, k, end, vert, pointpos;
double part, dx, xmin, xmax, ymin, ymax, y, x3, x4, y3, y4, len, maxlen = 0;
DPoint2d *vertices;
loopDir direction;
IntersectPoint mp1, mp2, point1, point2, points[MAXINTERSECTIONPOINTS];

xmin = psShape->dfXMin;
ymin = psShape->dfYMin;
xmax = psShape->dfXMax;
ymax = psShape->dfYMax;
part = (ymax-ymin)/(quality+1);
dx = xmax-xmin;
for(i=0;i<quality;i++)
{
y = ymin+part*(i+1);
pointpos = 0;
for(j=0;j<psShape->nParts;j++)
{
if(j==psShape->nParts-1)
end = psShape->nVertices;
else
end = psShape->panPartStart[j+1];vertices = new DPoint2d [end-psShape->panPartStart[j]];
for(k=psShape->panPartStart[j],vert=0;k<end;k++)
{
}
direction = LoopDirection(vertices, vert);
for(k=0;k<vert-1;k++)
{
y3 = vertices[k].y;
y4 = vertices[k+1].y;
if((y3 >= y && y4 < y) || (y3 <= y && y4 > y)) //I check >= only once, because if it's not checked now (y3) it will be in the next iteration (which is y4 now)
{
point1.boundry_nmb = j;
point1.loopdirection = direction;
x3 = vertices[k].x;
x4 = vertices[k+1].x;
if(y3==y)
{
point1.y = y3;
point1.x = x3;
if(direction == kInterior) //add point 2 times if the direction is interior, so that the final count of points is even
{
points[pointpos++]=point1;
}
}
else
{
point1.x = xmin+(((((x4-x3)*(y-y3))-((y4-y3)*(xmin-x3)))/((y4-y3)*dx))*dx); //striped down calculation of intersection of 2 lines
point1.y = y;
}
points[pointpos++]=point1;
}
}
delete [] vertices;
}

for(j=1;j<pointpos;j++) //sort the found intersection points by x value
{
for(k=j;k>0;k--)
{
if(points[k].x < points[k-1].x)
{
point1 = points[k];
points[k] = points[k-1];
points[k-1] = point1;
}
else
{
break;
}
}
}

for(j=0;j<pointpos-1;j++)
{
point1 = points[j];
point2 = points[j+1];
if((point1.loopdirection == kExterior         &&  //some checkings for valid point
point2.loopdirection == kExterior         &&
point1.boundry_nmb == point2.boundry_nmb  &&
j%2==0)                                   ||
(point1.loopdirection == kExterior        &&
point2.loopdirection == kInterior)        ||
(point1.loopdirection == kInterior        &&
point2.loopdirection == kExterior))
{
len = sqrt(pow(point1.x-point2.x, 2)+pow(point1.y-point2.y, 2));
if(len >= maxlen)
{
maxlen = len;
mp1 = point1;
mp2 = point2;
}
}
}
}

return DPoint2d((mp1.x+mp2.x)*0.5, (mp1.y+mp2.y)*0.5);
}

int main(int argc, char* argv[])
{
if(argc != 3)
{
printf("Usage: %s shpfile_path quality\n", argv[0]);
return 1;
}

int i, nEntities, quality;
SHPHandle hSHP;
SHPObject	*psShape;
DPoint2d pt;
quality = atoi(argv[2]);
hSHP = SHPOpen(argv[1], "rb");
SHPGetInfo(hSHP, &nEntities, NULL, NULL, NULL);

printf("PointInPoly v1.0, by Marko Podgorsek\n----------------\n");
for( i = 0; i < nEntities; i++ )
{
psShape = SHPReadObject( hSHP, i );
if(psShape->nSHPType == SHPT_POLYGON)
{
pt = CreatePointInPoly(psShape, quality);
printf("%d: x=%f y=%f\n",i, pt.x,pt.y);
}
SHPDestroyObject( psShape );
}

SHPClose(hSHP);

return 0;
}
```

This post has been edited by jimblumberg: 13 September 2012 - 08:18 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Is This A Good Question/Topic? 0

## Replies To: Code in C

### #2 jimblumberg

Reputation: 3055
• Posts: 9,292
• Joined: 25-December 09

## Re: Code in C

Posted 13 September 2012 - 08:20 PM

If you are getting compile errors, post the complete error messages exactly as they appear in your development environment.

Jim

### #3 baavgai

• Dreaming Coder

Reputation: 4888
• Posts: 11,282
• Joined: 16-October 07

## Re: Code in C

Posted 14 September 2012 - 03:48 AM

You say C, but your structs say C++. If it really is C, then you don't get to put constructors in your structs.

You also don't get to reference them without a "struct" in front of the name. You must use a typedef.