Server Help

Trash Talk - evil floating-point math

Cyan~Fire - Sat Oct 29, 2005 12:58 pm
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:
Code: Show/Hide
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.
Cerium - Sat Oct 29, 2005 1:09 pm
Post subject:
Whats the problem? Is that dotted line grid not supposed to be there?
Mine GO BOOM - Sat Oct 29, 2005 1:12 pm
Post subject:
Code: Show/Hide
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.
Cyan~Fire - Sat Oct 29, 2005 3:15 pm
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.
Cyan~Fire - Sat Oct 29, 2005 3:55 pm
Post subject:
Cancel that, it was just a typo, should be addition the first time. Same problem though, it still skips points. icon_sad.gif

Workaround/fix: Made scale 1:root2, thus multiplying out all the square roots of two in the floats.
D1st0rt - Sat Oct 29, 2005 9:26 pm
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...
Mr Ekted - Sat Oct 29, 2005 11:22 pm
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.
Cyan~Fire - Sun Oct 30, 2005 12:29 am
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. icon_biggrin.gif
Cerium - Mon Oct 31, 2005 1:54 pm
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.
Quan Chi2 - Mon Oct 31, 2005 4:52 pm
Post subject:
can you tell me what game it is? biggrin.gif
Cyan~Fire - Tue Nov 01, 2005 3:42 pm
Post subject:
All the information you'd want is here.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group