use already implemented line class functionality for best experience 👌
This commit is contained in:
parent
d1efc75f21
commit
bda1158e7f
15
Display.cpp
15
Display.cpp
@ -211,9 +211,14 @@ void Display::update ()
|
||||
int positives = 0;
|
||||
for (auto& pt : m_points)
|
||||
{
|
||||
positives += (sign(pt.getPosition().x, pt.getPosition().y,
|
||||
if (cand.is_point_right(Point(pt.getPosition().x, pt.getPosition().y)))
|
||||
{
|
||||
positives++;
|
||||
break; // sufficient as candidate if at least one point is right
|
||||
}
|
||||
/*positives += (sign(pt.getPosition().x, pt.getPosition().y,
|
||||
cand.from().x(), cand.from().y(),
|
||||
cand.to().x(), cand.to().y()) > 0) ? 1 : 0;
|
||||
cand.to().x(), cand.to().y()) > 0) ? 1 : 0;*/
|
||||
}
|
||||
|
||||
if (positives > 0)
|
||||
@ -253,11 +258,7 @@ void Display::update ()
|
||||
if (m_points[i].getFillColor() == sf::Color::Green) continue;
|
||||
|
||||
sf::Vector2f cand = m_points[i].getPosition();
|
||||
float distance = pDistance(
|
||||
cand.x, cand.y,
|
||||
m_lines[m_curLineIdx].from().x(), m_lines[m_curLineIdx].from().y(),
|
||||
m_lines[m_curLineIdx].to().x(), m_lines[m_curLineIdx].from().y());
|
||||
|
||||
float distance = m_lines[m_curLineIdx].distance_squared_to(Point(cand.x, cand.y));
|
||||
if (distance > maxDistance)
|
||||
{
|
||||
i_cand = i;
|
||||
|
67
Utility.h
67
Utility.h
@ -22,71 +22,4 @@ static bool IsPointInTriangle(const Point &pt, const Point &p1, const Point &p2,
|
||||
|
||||
return !(has_neg && has_pos);
|
||||
}
|
||||
|
||||
static float pDistance(float x, float y, float x1, float y1, float x2, float y2) {
|
||||
|
||||
float A = x - x1;
|
||||
float B = y - y1;
|
||||
float C = x2 - x1;
|
||||
float D = y2 - y1;
|
||||
|
||||
float dot = A * C + B * D;
|
||||
float len_sq = C * C + D * D;
|
||||
float param = -1;
|
||||
if (len_sq != 0) // in case of 0 length line
|
||||
param = dot / len_sq;
|
||||
|
||||
float xx, yy;
|
||||
if (param < 0) {
|
||||
xx = x1;
|
||||
yy = y1;
|
||||
}
|
||||
else if (param > 1) {
|
||||
xx = x2;
|
||||
yy = y2;
|
||||
}
|
||||
else {
|
||||
xx = x1 + param * C;
|
||||
yy = y1 + param * D;
|
||||
}
|
||||
|
||||
float dx = x - xx;
|
||||
float dy = y - yy;
|
||||
return dx * dx + dy * dy;
|
||||
//return sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
/* not used anymore... or yet
|
||||
static bool SortForMinXMaxY (const Point& a, const Point& b)
|
||||
{
|
||||
if (a.x() != b.x())
|
||||
{
|
||||
return (a.x() < b.x());
|
||||
}
|
||||
return (a.y() > b.y());
|
||||
}
|
||||
|
||||
static bool SortForMinYMaxX(const Point& a, const Point& b)
|
||||
{
|
||||
if (a.y() != b.y())
|
||||
{
|
||||
return (a.x() < b.x());
|
||||
}
|
||||
return (a.y() > b.y());
|
||||
}
|
||||
|
||||
static void sortPoints (std::vector<Point>& pts)
|
||||
{
|
||||
std::sort(pts.begin(), pts.end(), SortForMinXMaxY);
|
||||
}
|
||||
|
||||
// TODO: what happens if all/more points are on hor/vert line? -> sort for x, than y should handle this
|
||||
static std::pair<Point, Point> getMinMaxX(std::vector<Point>& pts)
|
||||
{
|
||||
// TODO: check if already sorted? assume array is sorted? call sort utility function???
|
||||
//sortPoints(pts);
|
||||
std::sort(pts.begin(), pts.end(), SortForMinXMaxY);
|
||||
|
||||
return std::make_pair(pts[0], pts[pts.size() - 1]);
|
||||
}*/
|
||||
#endif // UTILITY_H
|
Loading…
x
Reference in New Issue
Block a user