I am about to start a program that will involve the creation of 2D objects (mainly lines). These objects will then need to be selected and altered in various ways. I'm looking for a library with which to create this program and SFML seems very simple to use, but I haven't found any tutorials or functions that deal with object selection. Also, it would have to permit selection off the vertices (that is, in the case of a line composed of two vertices, selection can be done by clicking the line itself, not just its vertices). Also, intersections would have to be dealt with and, worst of all, enclosed areas would have to be selectable (if you have four independent lines creating a square, the square would have to be selectable). Should there be an efficient method of dealing with intersections, creating the area would be simple(ish).
if(LineA_intersects_B && B_intersects_C && C_intersects_A)
{
sf::Shape Area;
Area.AddPoint(A_intersection_B);
Area.AddPoint(B_intersection_C);
Area.AddPoint(C_intersection_A);
}
That is, of course, pseudo-code.
However, I then face the same problem with such an area. I'd have to be able to select the area by clicking on it, not merely on its vertices.
I just want to know if SFML has some function I don't know of which'd make this problem simpler or if you guys have any clever ideas of how to do this with an efficient algorithm.
The only algorithms I can think of are:
(1) launching an imaginary vector in one direction until it hits either the page border (and is thus not enclosed) or until it hits a line, at which point it'd then have to follow the line until it hits it's final vertex (implying the point selected is not enclosed) or another line intersects it, in which case it'd follow that line. This process would be repeated until the vector reaches its starting point. However, doing this for each mouse click sounds hardly efficient.
(2) saving, along with each object, a mathematical description of it (in the case of a line, its equation; in the case of a polygon, a series of equations that represent its borders). The point would then be compared to the mathematical description of each existing object. This seems far more efficient, especially for smaller models. However, it'd also be very inefficient for larger models.
This would be done by creating a series of classes, such as this brutish example:
class Line
{
sf::Shape line;
pair <pair<float,float>,pair<float,float>> vertices;
bool f(pair<float,float>);
}
Where function f is the mathematical function that defines the line. Thus, by inputting the coordinates of the click, it returns true if the point is contained within the line.
However, as I said, this would also kinda stink.