GenericBase.h
// // GenericBase.h // SuperBalance1.1 // // Created by Bruce Yang on 7/26/11. // Copyright 2011 Home. All rights reserved. // #import "cocos2d.h" #import "BYLineII.h" // includes the definition of BYLine struct #import "GameConfig.h" @interface GenericBase : NSObject { } // 已知两点,求经过这两点的直线的方程 + (BYLine) getLine:(CGPoint)p0 anotherPoint:(CGPoint)p1; // 已知直线的斜率和所经过的一点,求直线的方程 + (BYLine) getLine:(CGPoint)point kParam:(float)aK; // 求两点之间的距离 + (float) get2PointsDistance:(CGPoint)p0 anotherPoint:(CGPoint)p1; // 求一点与其在某条直线上的投影点之间的距离 + (CGPoint) getProjectivePoint:(CGPoint)p lineParam:(BYLine)l; // 已知3角形的3个顶点,求某个顶点的内角弧度 + (float) getRadian:(CGPoint)p1 secondPoint:(CGPoint)p2 thirdPoint:(CGPoint)p3; // 一元二次方程求根公式。。。 + (float) getB1:(float)k bParam:(float)b; + (float) getB2:(float)k bParam:(float)b; // 已知多边形两条相邻边的直线方程、在宏中定义的圆角的半径,求出多边形某顶点的圆角曲线的圆心点坐标 + (CGPoint*) getCircleCenter:(BYLine)l1 lineParam:(BYLine)l2; // 已知两条直线,求两条直线的交点坐标 + (CGPoint) getCrossPoint:(BYLine)l1 lineParam:(BYLine)l2; // 给出一个点的集合和一条直线,求出与该直线距离最近的一个点 + (CGPoint) getNearestPoint:(CGPoint*)points lineParam:(BYLine)line; // 根据多边形某个顶点的圆角曲线圆心点,及其在两条相邻边上的投影点,由这3个点生成该顶点的圆角曲线点数组~ + (CGPoint*) genGroupPoints:(CGPoint)pCircle pointParam1:(CGPoint)pProj1 pointParam2:(CGPoint)pProj2; @endPolygonPointsGenerator.h
// // PointsGeneratorGeneric.h // SuperBalance1.1 // // Created by Bruce Yang on 7/26/11. // Copyright 2011 Home. All rights reserved. // #import "GenericBase.h" @interface PolygonPointsGenerator : NSObject { } + (CGPoint*) genPolygonPoints:(CGPoint*)points pointsCount:(int)inPCount; + (CGPoint*) handleUnit:(CGPoint)p0 secondPoint:(CGPoint)p1 thirdPoint:(CGPoint)p2; @end
PolygonPointsGenerator.mm
// // PointsGeneratorGeneric.mm // SuperBalance1.1 // // Created by Bruce Yang on 7/26/11. // Copyright 2011 Home. All rights reserved. // #import "PolygonPointsGenerator.h" @implementation PolygonPointsGenerator + (CGPoint*) genPolygonPoints:(CGPoint*)p pointsCount:(int)inPCount { int outPCount = inPCount*11 + 2; CGPoint *target = new CGPoint[outPCount]; int targetCurrentIndex = 0; CGPoint p0, p1, p2; for(int i = 0; i < inPCount; i ++) { p0 = p[i]; p1 = (i-1 == -1) ? p[inPCount-1] : p[i-1]; p2 = (i+1 < inPCount) ? p[i+1] : p[0]; CGPoint *pGroup = [PolygonPointsGenerator handleUnit:p0 secondPoint:p1 thirdPoint:p2]; for(int j = 0; j < 11; j ++) { target[targetCurrentIndex + j] = pGroup[j]; } delete pGroup; targetCurrentIndex += 11; } target[targetCurrentIndex++] = target[0]; target[targetCurrentIndex++] = target[1]; return target; } // 这里的3个点表示某顶点与前后相邻的两个多边形顶点 + (CGPoint*) handleUnit:(CGPoint)p0 secondPoint:(CGPoint)p1 thirdPoint:(CGPoint)p2 { BYLine l_p0_p1 = [GenericBase getLine:p0 anotherPoint:p1]; BYLine l_p0_p2 = [GenericBase getLine:p0 anotherPoint:p2]; CGPoint *circleCenters = [GenericBase getCircleCenter:l_p0_p1 lineParam:l_p0_p2]; BYLine l_p1_p2 = [GenericBase getLine:p1 anotherPoint:p2]; CGPoint circleCenter = [GenericBase getNearestPoint:circleCenters lineParam:l_p1_p2]; CGPoint projectivePoint1 = [GenericBase getProjectivePoint:circleCenter lineParam:l_p0_p1]; CGPoint projectivePoint2 = [GenericBase getProjectivePoint:circleCenter lineParam:l_p0_p2]; // 这里的3个顶点为某顶点的圆角曲线的圆心点,及其该圆心点在多边形相关边上的两个投影点 CGPoint *pGroup = [GenericBase genGroupPoints:circleCenter pointParam1:projectivePoint1 pointParam2:projectivePoint2]; return pGroup; } @end