Hi, I’m making an image sorting app with which you can click an image and then drag it somewhere else. One picture was an easy job, but this ofcourse won’t work when i rotate the image… So my question is: is there an easy way to select and then drag an image? Or do i have to do some math? (no problem but i figured that there must be an more efficient method.
The way I did mine was; look if the mouse is clicking and dragging inside the boundaries if true then use the difference of mousemovement to move the image. But as said calculating the boundaries of an rotated picture is a bit harder.
I’m sorry – it’s kind of a difficult problem but not impossible with the rotation. without rotation it’s very easy. we haven’t really implmented any kind of hit test system, but my guess is that there might be good opengl type ways to do this. my solution would be geometric -
you can easily do a point in polygon test, if you know those rotated points. For example, this function
int pnpoly(int npol, float *xp, float *yp, float x, float y) {
int i, j, c = 0;
for (i = 0, j = npol-1; i < npol; j = i++) {
if ((((yp[i] <= y) && (y < yp[j])) ||
((yp[j] <= y) && (y < yp[i]))) &&
(x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
c = !c;
}
return c;
}
tells you if the point is in the polygon or not.
this is a bit of effort, but you could do the following:
a) perform the rotation manually on 4 points
b) test for point inside polygon with those points using the above code
if you want to post some code i’d be happy to take a look at it –
yeah that point in polygon solution is pretty useful - I use it alot
another approach which might be easier would be to do the opengl selecting routine – I didn’t drink enough coffee to get my head around it this morning, but as I understand it, it’s useful for picking primives in opengl after translations, projection, etc: