Author |
Message |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sat Oct 29, 2005 12:58 pm Post maybe stupid Post subject: evil floating-point math |
 |
|
|
|
OK, I'm writing some code that takes a 2d array in a file and paints it into a window. The tricky part is that it needs to be displayed as a diamond, not a sqare. (It's from a computer game, the map uses N,E,S,W corners.) Here's my rotation function:
inline int round(double n)
{
return int((n >= 0.0) ? n + 0.5 : n - 0.5);
}
#define pi 3.1415926536
/* rotates points 45deg to make a diamond from a square */
inline void rotate(int half, int x, int y, int &rx, int &ry)
{
double r, theta;
/* Make center origin */
x -= half;
y = half - y;
r = sqrt(x * x + y * y);
theta = atan2(y, x); //sweet!
theta += pi / 4;
/* Convert back to upper-left origin */
rx = round(r * cos(theta)) + half;
ry = round(r * sin(theta)) + half;
} |
But what I'm getting is this:
I'm sure it's something obvious but I've been thinking about it all day and can't seem to figure out what's going wrong. Any clues? If you need more information, just tell me. _________________ This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him.
evil
mapview.png - 10.42 KB
File downloaded or viewed 19 time(s)
|
|
Back to top |
|
 |
Cerium Server Help Squatter

Age:42 Gender: Joined: Mar 05 2005 Posts: 807 Location: I will stab you. Offline
|
Posted: Sat Oct 29, 2005 1:09 pm Post maybe stupid Post subject: |
 |
|
|
|
Whats the problem? Is that dotted line grid not supposed to be there? _________________ There are 7 user(s) ignoring me right now.
|
|
Back to top |
|
 |
Mine GO BOOM Hunch Hunch What What

Age:41 Gender: Joined: Aug 01 2002 Posts: 3615 Location: Las Vegas Offline
|
Posted: Sat Oct 29, 2005 1:12 pm Post maybe stupid Post subject: |
 |
|
|
|
void rotate(int px, int py, int xi, int yi, int &xo, int &yo, int angle)
{
/* convert to radians */
double ang = round((double)angle * 3.1415926535898 / 180.0);
xo = px + round((double)(xi - px) * cos(ang) - (double)(yi - py) * sin(ang));
yo = py + round((double)(xi - px) * sin(ang) - (double)(yi - py) * cos(ang));
} |
Try that. P# is the pivot point, #I is the initial point, and #O is the output. Since you like angles being degrees, added the degrees->radians calculations as thats what sin/cos like.
If you know the function will convert the map 45 degrees everytime, you can design a function to convert pixels much, much faster since you know what the angle will be everytime, thus can have sin/cos precalculated.
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sat Oct 29, 2005 3:15 pm Post maybe stupid Post subject: |
 |
|
|
|
Huh? I tried plugging that func in and all it did was draw a straight line, which makes sense since cos(pi/4) and sin(pi/4) are equal.
@Cerium: That "dotted line grid" is actually not a dotted line grid. The missing points are not evenly placed.
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sat Oct 29, 2005 3:55 pm Post maybe stupid Post subject: |
 |
|
|
|
Cancel that, it was just a typo, should be addition the first time. Same problem though, it still skips points.
Workaround/fix: Made scale 1:root2, thus multiplying out all the square roots of two in the floats.
|
|
Back to top |
|
 |
D1st0rt Miss Directed Wannabe

Age:37 Gender: Joined: Aug 31 2003 Posts: 2247 Location: Blacksburg, VA Offline
|
Posted: Sat Oct 29, 2005 9:26 pm Post maybe stupid Post subject: |
 |
|
|
|
SIM CITY 2000!!!!!!!!!!!
(I know that's not actually what it is but thats what I initially thought of and it brought back such fond memories)
On a side note, I lost my sc2k cd if anybody still has a copy lying around... _________________
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Sat Oct 29, 2005 11:22 pm Post maybe stupid Post subject: |
 |
|
|
|
This is an image scaling issue. When you make something larger/smaller by a non-multiple/divisor, you need to interpolate values. This can be extremenly complex in rotated space. The proper method (one that looks the best) is bicubic, but it's expenisve. _________________ 4,691 irradiated haggis!
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sun Oct 30, 2005 12:29 am Post maybe stupid Post subject: |
 |
|
|
|
Yeah, I remember reading about that some time when wondering why image rotations in MS Photo Editor looked like crap. So, uhhh, thanks for the science, but I doubt whether I'm going to do anything that special in my program.
|
|
Back to top |
|
 |
Cerium Server Help Squatter

Age:42 Gender: Joined: Mar 05 2005 Posts: 807 Location: I will stab you. Offline
|
Posted: Mon Oct 31, 2005 1:54 pm Post maybe stupid Post subject: |
 |
|
|
|
Cyan~Fire wrote: | Huh? I tried plugging that func in and all it did was draw a straight line, which makes sense since cos(pi/4) and sin(pi/4) are equal.
@Cerium: That "dotted line grid" is actually not a dotted line grid. The missing points are not evenly placed. |
Oh.
Sitting back about 3 feet from my monitor at 2048x1536 it looked like it was supposed to be there, so I didnt even look through the code. Sorry.
|
|
Back to top |
|
 |
Quan Chi2 Member of "Sexy Teenagers that Code" Group

Age:34 Gender: Joined: Mar 25 2005 Posts: 860 Location: NYC Offline
|
Posted: Mon Oct 31, 2005 4:52 pm Post maybe stupid Post subject: |
 |
|
|
|
can you tell me what game it is?
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Tue Nov 01, 2005 3:42 pm Post maybe stupid Post subject: |
 |
|
|
|
All the information you'd want is here.
|
|
Back to top |
|
 |
|