# 根据凸多边形顶点坐标来计算面积算法与实现

-(void) drawGrids {

CGSize size= [[CCDirector sharedDirector] winSize];

floatwinHeight = size.height;

floatwinWidth = size.width;

intverCount = winWidth / PTM_RATIO;

inthorCount = size.width / PTM_RATIO;

for (int i= 1; i < verCount-5; i++) {

ccDrawLine(ccp(0,i*PTM_RATIO), ccp(winWidth, i*PTM_RATIO));

}

for (int i= 1; i < horCount; i++) {

ccDrawLine(ccp(i*PTM_RATIO,0), ccp(i*PTM_RATIO, winHeight));

}

}

-(float)calculatePolygonArea:(NSMutableArray*) vertexes {

//    vertexes= [self reorderVertexes:vertexes];

floatresult = 0;

intvertexCount = [vertexes count];

CGPointstartPoint = [vertexes[0] CGPointValue];

for (int i= 1; i < vertexCount - 1; i++) {

result+= [self calculateTriangleArea:startPoint pointB:[vertexes[i] CGPointValue]pointC:[vertexes[i+1] CGPointValue]];

}

returnresult;

}

-(float)calculateTriangleArea:(CGPoint) pointA

pointB:(CGPoint)pointB

pointC:(CGPoint)pointC {

floatresult =  (pointA.x * pointB.y + pointB.x * pointC.y + pointC.x *pointA.y

-pointA.y * pointB.x - pointB.y * pointC.x - pointC.y * pointA.x) * 0.5f;

returnresult > 0 ? result : -result;

}

• 本文已收录于以下专栏：

举报原因： 您举报文章：根据凸多边形顶点坐标来计算面积算法与实现 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)