简体   繁体   中英

Constructing a rectangle from smaller rectangles

For the last few hours I've been attempting to construct a rectangle from smaller rectangles on any angle; I imagined doing so like this http://i.stack.imgur.com/Ymakk.png

[ Where the black lines represent the outline of the constructed rectangle and the red lines represent the triangles that construct it. The red rectangles have to be completely straight also. ]

Obviously it wouldn't look very defined unless I have a very small increment when trying to render it but the end product would hopefully look like a rectangle at a predetermined angle.

The screen is set up so that the top, left hand corner is (0, 0) and the bottom, right is (1, 1). How would I do this? Not specifically looking for actual code, pseudo-code or even an explanation, maybe some math.. would be brilliant! Thanks in advance.

Here's a bit of geometry for you:

If your rectangle is perfectly aligned, you can just draw one big rectangle and you win.

If it's slighted at any angle at all, you can express the lines as simple equations

y = m * x + c

where you insert x and y of both edges to calculate m and c (a very simple procedure actually), or you can calculate that m = tan(angle)

Then you can "just" walk down the y coordinates and get the corresponding x positions on the left and right lines that belong to the rectangle by inserting the y, c and m values to the equation and solving for x.

If you look at your rectangle picture, you can see that there's basically three parts: the first part goes from the bottom to the lower left edge. It follows the long side to the left and encounters an edge on the left side. After that you follow both short sides to the right and then you encounter the upper right edge and go up to the upper left corner (of course you could do this from the top down, too)

You'll need to carefully check if you've reached the edge between two lines on the left or right side and switch your values for m and c at the appropiate moments.

I think this should get you close to a solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM