SFML community forums

Help => Graphics => Topic started by: felaugmar on November 05, 2016, 06:06:01 pm

Title: [Solved] Transform - Oriented Rectangle
Post by: felaugmar on November 05, 2016, 06:06:01 pm
Hello,

I know that the 'Transform class' we have in SFML don't have the option to return an oriented rectangle but returns a bounding rectangle which is nice;
But, you guys know any algorithm that returns an array of Points instead?

btw, for now I'm fine with the bounding, I'm just wondering which algorithm can be applied for this.

Thank you.
Title: Re: Transform - Oriented Rectangle
Post by: Hapax on November 05, 2016, 09:25:18 pm
An array of points? Do you mean the corners of the rectangle after transformations?
You can use the object's transform to transform the points yourself. The point without the transform can be found from the local bounding rectangle.

You could try something like this (untested):
sf::Transform transform = object.getTransform();
sf::FloatRect localRect = object.getLocalBounds();
std::vector<sf::Vector2f> points(4);
points[0] = transform.transformPoint(sf::Vector2f(localRect.left, localRect.top));
points[1] = transform.transformPoint(sf::Vector2f(localRect.left + localRect.width, localRect.top));
points[2] = transform.transformPoint(sf::Vector2f(localRect.left + localRect.width, localRect.top + localRect.height));
points[3] = transform.transformPoint(sf::Vector2f(localRect.left, localRect.top + localRect.height));
Title: Re: Transform - Oriented Rectangle
Post by: felaugmar on November 05, 2016, 10:13:23 pm
What I'm looking after is a vector with all the points inside the area of the rectangle.
something like this \/

sf::Vector2f p1 {10.f, 0}; // top-left point
sf::Vector2f p2 {0, 10.f}; // bottom-left point
sf::Vector2f p3 {10.f, 20.f}; // bottom-right point
sf::Vector2f p4 {20.f, 10.f}; // top-right point

//  The 4 points above would make a rectangle at 45 degrees

std::vector<sf::Vector2i> rectangleAreaPoints = getPointsInsideRectangle(p1, p2, p3, p4);
 

I attached an image, well, I want all the pixels in the black area.
;D
Title: AW: Transform - Oriented Rectangle
Post by: eXpl0it3r on November 05, 2016, 10:25:09 pm
Render to a render texture, copy it to an image and access the image data through the pixel pointer.

Alternativly you can do a point transformation for every point of your rectangle.

Question is, what do you need this for?

If you need an "algorithm" then just look at some transformation math.
Title: Re: Transform - Oriented Rectangle
Post by: felaugmar on November 05, 2016, 11:38:47 pm
I'm maping objects that is appearing in the screen for "mouse collision";
that is, I'm doing a big "2D vector", with the size of the screen,
for example, 1280x720;
Then there's "handlers" in each pixel to some kind of function call (onHover, onStopHover) of some object.

Well, doing point transformation of every point is a way, cool.
Title: Re: Transform - Oriented Rectangle
Post by: Hapax on November 06, 2016, 12:06:49 am
All pixels inside a transformed rectangle? S.A.T..
You could test a grid (to match the pixels) to see if they are inside the rectangle, outside the rectangle or contain an edge. You might only need line-crosses-line tests for this.

A simple but slow way would be to render to a render texture, copy to image, and test the pixels ;)