HGeometryUtils.cpp
4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// HGeometryUtils.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 20.05.2017.
//
//
#include <stdio.h>
#include "HGeometryUtils.h"
//TODO: use original scale doesn't seem to work
cocos2d::Rect HGeometryUtils::getBoundingBoxToWorld(cocos2d::Node* node, bool ignoreTransformations)
{
auto tempScale = node->getScale();
auto tempRotation = node->getRotation();
if(ignoreTransformations){
node->setScale(1);
node->setRotation(0);
}
auto bb = node->getBoundingBox();
bb.origin = cocos2d::Point(0,0);
auto bbToWorld = cocos2d::RectApplyTransform(bb, node->getNodeToWorldTransform());
if(ignoreTransformations){
node->setScale(tempScale);
node->setRotation(tempRotation);
}
return bbToWorld;
}
cocos2d::Rect HGeometryUtils::getRectIntersection(cocos2d::Rect rect1, cocos2d::Rect rect2)
{
auto intersection = cocos2d::Rect(MAX(rect1.origin.x, rect2.origin.x), MAX(rect1.origin.y, rect2.origin.y), 0, 0);
intersection.size.width = MIN(rect1.origin.x + rect1.size.width, rect2.origin.x + rect2.size.width) - intersection.origin.x;
intersection.size.height = MIN(rect1.origin.y + rect1.size.height, rect2.origin.y + rect2.size.height) - intersection.origin.y;
return intersection;
}
bool HGeometryUtils::segmentIntersectsRect(cocos2d::Rect rect, cocos2d::Point lineP1, cocos2d::Point lineP2)
{
if(rect.containsPoint(lineP1) || rect.containsPoint(lineP2)){
return true;
}
auto rectX2 = rect.origin.x + rect.size.width;
auto rectY2 = rect.origin.y + rect.size.height;
if((lineP1.x > rectX2 && lineP2.x > rectX2) || (lineP1.x < rect.origin.x && lineP2.x < rect.origin.x) || (lineP1.y > rectY2 && lineP2.y > rectY2) || (lineP1.y < rect.origin.y && lineP2.y < rect.origin.y)){
return false;
}
if(lineP1.y == lineP2.y){
return true;
}
if(lineP1.x == lineP2.x){
return true;
}
auto lineA = (lineP1.y - lineP2.y)/(lineP1.x - lineP2.x);
auto lineB = lineP1.y - lineP1.x * lineA;
auto yr1 = rect.origin.x*lineA + lineB;
auto yr2 = (rect.origin.x+rect.size.width)*lineA + lineB;
auto xr1 = (rect.origin.y - lineB)/lineA;
auto xr2 = (rect.origin.y+rect.size.height - lineB)/lineA;
return (yr1 >= rect.origin.y && yr1 <= rectY2) || (yr2 >= rect.origin.y && yr2 <= rectY2) || (xr1 >= rect.origin.x && xr1 <= rectX2) || (xr2 >= rect.origin.x && xr2 <= rectX2);
}
cocos2d::Point HGeometryUtils::addPoints(std::vector<cocos2d::Point> points)
{
float sumX = 0, sumY = 0;
for(int i = 0; i < points.size(); ++i){
sumX += points[i].x;
sumY += points[i].y;
}
return cocos2d::Point(sumX, sumY);
}
void HGeometryUtils::getLineABCFromPoints(cocos2d::Point pointA, cocos2d::Point pointB, float (&line)[3])
{
auto A = pointA.y - pointB.y;
auto B = pointB.x - pointA.x;
auto C = pointA.x * pointB.y - pointB.x * pointA.y;
line[0] = A;
line[1] = B;
line[2] = C;
}
bool HGeometryUtils::segmentCrossesLine(cocos2d::Point sPointA, cocos2d::Point sPointB, float lineA, float lineB, float lineC)
{
auto valueA = lineA * sPointA.x + lineB * sPointA.y + lineC;
auto valueB = lineA * sPointB.x + lineB * sPointB.y + lineC;
if(valueA * valueB <= 0){
return true;
}
return false;
}
float HGeometryUtils::calculateIntersectionPercentage(cocos2d::Rect rect1, cocos2d::Rect rect2, int percentageOfWhichRect)
{
if(rect1.size.width == 0 || rect1.size.height == 0 || rect2.size.width == 0 || rect2.size.height == 0){
return 0;
}
if(!rect1.intersectsRect(rect2)){
return 0;
}
auto intersection = HGeometryUtils::getRectIntersection(rect1, rect2);
auto r = percentageOfWhichRect == 0 ? rect1 : rect2;
return intersection.size.width * intersection.size.height / (r.size.width * r.size.height);
}
/*
// relatively to line y = 0
lineAngle : function(lineP1, lineP2){
if(lineP1.x == lineP2.x){
return 0;
} else if(lineP1.y == lineP2.y){
return Math.PI/2;
}
// build a triangle, with third point equal to (x1, y2), and get the arc sin of (p3,p2)/(p1,p2)
let point3 = new cc.p(lineP1.x, lineP2.y);
let angle = Math.asin(cc.pDistance(lineP2, point3)/cc.pDistance(lineP1, lineP2));
// add a multiple of PI/2, depending on the quarter
if(lineP2.y < lineP1.y){
if(lineP2.x > lineP1.x){
angle = Math.PI - angle;
} else {
angle = Math.PI + angle;
}
} else if(lineP2.x < lineP1.x){
angle = Math.PI*2 - angle;
}
return angle;
}
};
*/