cocos2dx 判断点是否在任意凸多边形内(算法)

      工作不忙,无聊,上次老大说,后来可能要用到这个算法,所以写了这个代码,代码很短!

我也新手来着,可能会有BUG,发现问题,请联系我QQ,164453625,问题答案:我没有车

不喜勿喷,欢迎提建议!!


直接上代码咯,不废话了!

点。h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include <vector>
using namespace std;
using namespace cocos2d;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();  
    static cocos2d::CCScene* scene();
    void menuCloseCallback(CCObject* pSender);

	virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
	virtual void registerWithTouchDispatcher();


    CREATE_FUNC(HelloWorld);
private:
	//画三角形
	void drawShape(vector<CCPoint> vecPoint);
	//判断点是否在三角形里
	bool isContain(CCPoint point);
	//判断点是否满足直线
	bool isMeetLine(CCPoint point1,CCPoint point2,CCPoint point,CCPoint z);
	//
	float isLine(CCPoint point1,CCPoint point2,CCPoint z);
private:
	vector<CCPoint> vecPoint; 
};

#endif // __HELLOWORLD_SCENE_H__


cpp


#include "HelloWorldScene.h"

USING_NS_CC;


CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);
   
	vecPoint.push_back(CCPointMake(100,30));
	vecPoint.push_back(CCPointMake(50,70));
	vecPoint.push_back(CCPointMake(80,130));
	vecPoint.push_back(CCPointMake(150,130));
	vecPoint.push_back(CCPointMake(200,130));
	vecPoint.push_back(CCPointMake(150,30));

	drawShape(vecPoint);


	this->setTouchEnabled(true);
    return true;
}

void HelloWorld::drawShape(vector<CCPoint> vecPoint){
	CCDrawNode* drawNode = CCDrawNode::create();
	this->addChild(drawNode);
	ccColor4F color = {1.0f,0.0f,0.0f,1.0f};
	int size = vecPoint.size();
	for (int i = 0; i < size ;i++)
	{
		if (i == size -1)
		{
			drawNode->drawSegment(vecPoint[i],vecPoint[0],1,color);
		}else{
			drawNode->drawSegment(vecPoint[i],vecPoint[i+1],1,color);
		}
	}
}


//判断点是否在三角形里
bool HelloWorld::isContain(CCPoint point){
	bool bRet = true;
	int size = vecPoint.size();
	for (int i = 0;i < size; i++)
	{
		if (i == size - 1)
		{
			if(!isMeetLine(vecPoint[i],vecPoint[0],point,vecPoint[1])){
				bRet = false;
				break;
			}
		}else if (i == size - 2)
		{
			if(!isMeetLine(vecPoint[i],vecPoint[i+1],point,vecPoint[0])){
				bRet = false;
				break;
			}
		}else{
			if(!isMeetLine(vecPoint[i],vecPoint[i+1],point,vecPoint[i+2])){
				bRet = false;
				break;
			}
		}
	}
	return bRet;
}

float HelloWorld::isLine(CCPoint point1,CCPoint point2,CCPoint z){
	float number1 = point2.x - point1.x;
	float number2 = z.y - point1.y;
	float number3 = point2.y - point1.y;
	float number4 = z.x - point1.x;
	float number5 = number1 * number2 - number3 * number4;
	return number5;
}
bool HelloWorld::isMeetLine(CCPoint point1,CCPoint point2,CCPoint point,CCPoint z){

	bool bRet = false;								
	if (point1.x == point2.x && point1.y != point2.y)	
	{
		if ((z.x >point1.x && point.x > point1.x) || (z.x < point1.x && point.x < point1.x) || z.x == point1.x)
		{
			bRet = true;
		}
		return bRet;
	}
	if (point1.y == point2.y && point1.x != point2.x)
	{

		if ((z.y > point1.y && point.y > point1.y) || (z.y < point1.y && point.y < point1.y) || z.y == point1.y)
		{
			bRet = true;
		}
		return bRet;
	}
	
	
	if((isLine(point1,point2,z) >= 0 && isLine(point1,point2,point) >= 0) || (isLine(point1,point2,z) < 0 && isLine(point1,point2,point) < 0)){	
		bRet = true;
	}else {
		bRet = false;
	}
	return bRet;
}

bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
	//CCLog("11111111111111");
	return true;
}
void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){}
void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){
	CCPoint point = pTouch->getLocation();
	if (isContain(point))
	{
		CCLog("wo ai ni ");
	}
	
	CCLog("%f,%f",point.x,point.y);
}
void HelloWorld::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent){}

void HelloWorld::registerWithTouchDispatcher(){
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}

源码下载地址!http://pan.baidu.com/s/1ntDKoVf



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值