Rectangle - a mathematical problem - math

I have found something NOT funny with rectangles:
Lets say, given are values of left, top, right and bottom coordinates and all those coordinates are intended to be inclusive.
So, calculating the width goes like:
width = right - left + 1
So far, so logical. But!
A width of zero (which makes sense, sometimes) would have to be stored as:
right = left - 1
which makes problems, when it comes to the following operations:
Sorting the rectangle coordinates (to make it go left to right, top to bottom)
Looping
Ok, of course those things can be handled with extra code for the special case of Width == 0, but, seriously, is there no better solution, no standard pattern or best practice to handle this?
Edit:
For the time being I have abandoned the "sorting" of the coordinates in my code and replaced it with an assertion stating that the rectangle must be left -> right, up -> down, but seriously...

To address this problem, most graphics libraries will draw rectangles from the left coordinate up to but not including the right coordinate. So if left=10 and right=20, then the ten pixels 10 through 19 will be drawn.
You can think of this as the pixel coordinate referring not to the lit-up portion, but the grid lines between pixels.
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
^ ^ ^ ^
0 1 2 3

It's important to distinguish between coordinates and pixels. You can think of the coordinate system as being an invisible grid which runs between pixels. Thinking of coordinates this way if you define a rect as { 0, 3, 0, 5 }, then you get 3 pixels by 5 pixels as expected.
| | | | | |
0 -x--+--+--+--+--x-
| | | | | |
1 -+--+--+--+--+--+-
| | | | | | <- pixels are rectangular areas between coordinate grid
2 -+--+--+--+--+--+-
| | | | | |
3 -x--+--+--+--+--x-
| | | | | |
0 1 2 3 4 5

If the edges (left, right, top, bottom) are inclusive then, by definition, the width (and height) of the rectangle cannot be 0. By "including" the side (which is a pixel), you're saying that it has to be at least 1 pixel wide.

Saying that
all those coordinates are intended to be inclusive
means that there actually are two distinct rectangles, one within another. That's where you get caught: when you write
width = right - left + 1
it really means:
inner_width = outer_right - outer_left + thickness
where thickness is the distance between corresponding sides of inner and outer rectangles.
So, to deal with the problem in abstract mathematical sense, you have to consider two rectangles instead of one.

Of course you can find a workaround for this, but what really is the problem here is going out of scope.
Your scope is a rectangle, and even if a width of zero would come in handy : there is no such thing as a rectangle with width zero.
Normally all functions have contracts and a predcondition of a functions that says docalculation(par_rectangle) is that par_rectangle in fact is a rectangle.
If you need a retangle-like object wich can be width zero, you first need to define it waterproof, and never just assert that rules for rectangles will apply on your definiton.

Related

Determining the adjacent face of a cube for a given direction (up, down, left or right)

I have a procedurally created cube. I have a reference to each of the six faces which I call front, back, left, right, top and bottom.
________
/ /|
/ T / |
|--------| R|
| | |
| F | /
|________|/
Each face has a procedurally created texture. The texture is square and is created by iterating the pixels of a pre-defined resolution size (width == height == resolution size).
The x coordinate of the pixel starts from left and increases to the right along the texture.
The y coordinate of the pixel starts from the bottom and increases upwards of the texture.
I am now creating a heightmap texture using averaging of the pixels around a given pixel. For this to work, on the occasions when a specific pixel is on the edge of a cubes face, I will need to access the pixel next to it which lies on another face.
My question: Is there a fast, easy way to determine an adjacent face for a given direction (up, down, left or right) starting from any face?
For example: Say I am dealing with the pixels on the Front face, and am iterating the pixels on the right-hand edge (where x == resolution). I know that I need to grab the adjacent pixel on the Right face, because if I walk to the right of the Front face I wrap around onto the Right face.
I could write the logic as a big bunch of conditional checks for every face, covering every scenario, but I'm sure there must be a more elegant way to achieve this?

Splitting Ordered Array of Items to Alternating Columns in HTML

I'm trying to create a responsive HTML layout which will display an array of ordered data.
On a smaller screen size, it will display one column of content in order. However, on a larger screen size, it'd display two columns with the items alternating between the two columns.
Ex: Small
| 1 |
| 2 |
| 3 |
| 4 |
Ex. Large
| 1 | 2 |
| 3 | 4 |
Adding onto this, in the two column layout, the elements in the left column should be float: right, while the right column should be float: left so that they meet up in the center no matter the width of the elements.
The only idea I've come up with so far is to create two column containers, one that will float all elements right and one that floats all elements left. However, I have yet to figure out how to distribute the items in the order array so that I won't have to split it up into two, since that will mean that when changing screen sizes to the smaller one, the one column of items would no longer be in order.
Edit 1: Each element may not have the same height, and when placing the element, it should go onto the height that has the smaller overall height.
| ------|----------|
| | 1 | 2 |
| | |----------|
| | | 3 | |
| ------|------- |
This is a sample jsfiddle of approximately what I'm trying to do, except perhaps less-hacky. The layout I'm trying to achieve is similar to this, except the elements have a specific order.
So after some testing, a friend and I decided that this was probably the best way was to use display: flex on left elements while using float: right on the right elements as shown in this jsfiddle.

Converting a 3D position to a 2D screen position

I am trying to create a screen overlay for an existing computer game. This overlay should display icons that show certain locations in the 3D world. Now because I do not have access to the data I want to be able to get the screen positions, I have to figure it out using the following variables:
Player position (x,y,z)
Camera position (x,y,z)
Point position (x,y,z)
Camera Angle(FOV)
Using these variables I managed to get the right and left edge of the camera view. Using these 2 variables I am able to get the point between 0 and 1 where the icon should be displayed on the x axis. (0 is on the left edge, 1 is right edge) This seems to work quite well, but only if I am aligned to either the X or Z axis, as shown in the following example: http://i.imgur.com/OIZWBME.png
The problem is, I have no clue on how to make this work when the camera rotates. I've tried combining the 2 points that work alone, but also with other variables like the camera-player world rotation and angles.
Does anyone have any ideas on how I might be able to make this work? If I forgot to supply any info please let me know!
Tim
There are pretty good articles on wikipedia (https://en.wikipedia.org/wiki/3D_projection), and other tutorials online (http://www.codeproject.com/Articles/2158/A-short-discussion-on-mapping-3D-objects-to-a-2D-d).
Essentially, for this simple instance, it boils down to rotating everything in a virtual space before rendering. The points you want to draw are moved in relation to the camera position, so that the camera becomes the new axis point, and then you can draw as before.
A simple top down ascii-art example:
( []< is the camera, a, b, and c are points)
What you can already draw:
^ <-----y------>
|
| b
| []< a
x
| c
|
|
v
What you can't draw: (the camera moved and rotated)
<-----y------>
^ [ ]
| ^
| b
x a
| c
|
v
So you need to map (usually using matrixies) the camera's global position against the positions of everything else, so that you end up with:
After transformation:
<---------fake y------>
^
| a
|
fake | b
x: | []<
| c
|
v
Which you can now draw as normal. I'm not an expert on this, but does this help to get you started?

How to cut a rectangle sheet into half? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
We have rectangle sheet, and a small rectangle piece cut inside it at RANDOM place. How to make make that sheet into exact same 2 halves?
If you do a cut through the center of a rectangle, no matter at what angle, this rectangle will be divided in half.
Thus, if we make a cut through the center of the cutout rectangle, each side of the cut will have 50% of the cutout area. And if we -- at the same time -- cut through the center of the large rectangle, this should do it. Both sides of the cut have half the area of the large rectangle, minus half the size of the cutout.
Of course, that is if by "exact same 2 halves" you mean same area, but not necessarily same shape. The latter will generally not be possible.
re: folding - will not work because you could easily end up with more than two parts cutting folded rectangle.
I would try brute-force approach. If the hole is co-oriented to large rectangle, you can cut along one of the edges and have only three cases to check: 1 try to cut above the hole - if it's not possible to end with same area halves, try to 2 cut through the hole - if it's still not possible, 3 cut below the hole will certainly do it. the code for 1 and 3 would be very similar, btw. all 3 cases are simple linear equations for the height of cut. For example, 1:
+----W---+
d |
+--------+
| |
| +-w-+ |
H h | |
| +---+ |
| |
| |
+--------+
We have d * W = (H - d) * W - h * w
If d from this equation is not above the hole, go to case 2, and so on.
After a lot of thought, I think this is the correct answer.
We have to fold the paper sheet, many times, so that it should form the rectangle of the small rectangle area.
If we cut the folded one, it should give 2 equal parts definitely.

How to check if any point (or part) of a line is inside or touches a rectangle

I want to check if a line (or any point of a line) is within a rectangle or intersects a rectangle.
I have (x0, y0) and (x1, y1) as starting and ending points of a line.
Also, (ax,ay) and (bx,by) as the top-left and bottom-right points of a rectangle
For example,
____________
| |
---|----- | Result: true
| |
|____________|
/
_/__________
|/ |
/ | Result: true
/| |
|____________|
____________
| |
| -------- | Result: true
| |
|____________| ---------- Result: false
Can anyone suggest how to do this? I dont want to know which point is that, i just want to know if its there or not.
Thanks a lot for help
The first and third cases are trivial - simply return true if either endpoint of the line is within the box (i.e. > ax and ay, < bx and by).
The second presents a problem - we can't rely on the endpoints of our line anymore. In this case, we will have to test the line with each edge of the rectangle.
The equation for our line will be (x1 - x0)*x + (y1 - y0)*y + x0*y0 - x1*y1 = 0 , and we can construct a similar equation for each side of the rectangle using the corners. Following that, substituting the equation for the sides of the rectangle into our line will give us the intersection.
Finally, we check to ensure that the point is within the bounds of the side of the rectangle, and likewise within the line segment we are considering.
There is a more detailed account of this in this discussion.

Resources