Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
evil floating-point math

 
Post new topic   Reply to topic Printable version
 View previous topic  If the times on posts are one hour off... Post :: Post MPOG RPG Spaceships Free.  View next topic  
Author Message
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sat Oct 29, 2005 12:58 pm   Post maybe stupid    Post subject: evil floating-point math Reply to topic Reply with quote

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.
_________________
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
View users profile Send private message Add User to Ignore List Visit posters website
Cerium
Server Help Squatter


Age:42
Gender:Gender:Male
Joined: Mar 05 2005
Posts: 807
Location: I will stab you.
Offline

PostPosted: Sat Oct 29, 2005 1:09 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:41
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3615
Location: Las Vegas
Offline

PostPosted: Sat Oct 29, 2005 1:12 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List Send email
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sat Oct 29, 2005 3:15 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sat Oct 29, 2005 3:55 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
D1st0rt
Miss Directed Wannabe


Age:37
Gender:Gender:Male
Joined: Aug 31 2003
Posts: 2247
Location: Blacksburg, VA
Offline

PostPosted: Sat Oct 29, 2005 9:26 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sat Oct 29, 2005 11:22 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sun Oct 30, 2005 12:29 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Cerium
Server Help Squatter


Age:42
Gender:Gender:Male
Joined: Mar 05 2005
Posts: 807
Location: I will stab you.
Offline

PostPosted: Mon Oct 31, 2005 1:54 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address
Quan Chi2
Member of "Sexy Teenagers that Code" Group
Member of


Age:34
Gender:Gender:Male
Joined: Mar 25 2005
Posts: 860
Location: NYC
Offline

PostPosted: Mon Oct 31, 2005 4:52 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

can you tell me what game it is? biggrin.gif
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Tue Nov 01, 2005 3:42 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

All the information you'd want is here.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Trash Talk All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 169 page(s) served in previous 5 minutes.

phpBB Created this page in 0.682967 seconds : 37 queries executed (84.7%): GZIP compression disabled