delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/16/15:22:28

Message-Id: <m0xLvQd-0003LgC@fwd03.btx.dtag.de>
Date: Thu, 16 Oct 97 21:17 MET DST
To: joshuahab AT aol DOT com, djgpp AT delorie DOT com
References: <19971015041101 DOT AAA17302 AT ladder01 DOT news DOT aol DOT com>
Subject: Re: Newbie seeks help with rotating triangle program
MIME-Version: 1.0
From: Georg DOT Kolling AT t-online DOT de (Georg Kolling)

Joshua Hab schrieb:
> Hi everyone, I'm relatively new to programming and very new to DJGPP, and I
>  was
>  wondering if some kind soul(s) would help me with a program I'm writing that
>  rotates a triangle (just an outline though) around the center of the screen
>  (also the triangle's center)
>
> The problem I'm having is that the function I wrote to determine the x and y
>  values of the vertices is returning a value of 210 for x and 100 for y no
>  matter what I put into them. The formula I'm using is as follows:
>  (RADIUS_LENGTH is the radius of the circumscribed circle and CENTER_X is the
>  center of it)

[some code snipped]

> #define CENTER_X 160
> #define CENTER_Y 100
> #define RADIUS_LENGTH 50

Why do you define them if you don't use them in your code ??

> void main()
> {
>    int x1, x2, x3, y1, y2, y3;
>    //x and y coordinates for the 3 points of a triangle
>
>    int theta1=90, theta2=210, theta3=330; //rotation in degrees

[again snipped]

> calculate_x(theta)
> int theta;
> {
>     int x=0;
>     x=(50*(cos(theta/360*6.28)))+160; //our equation for determining x
>     return(x);
> }

this code is a good example for the use of casts!
first, what the code actually does:

   theta/360   result: 1       theta is an int, so a division without explicit
                               cast will also result in a (up)rounded int
   1 * 6.28    result: 6.28    6.28 is definitely not an int, and gcc will 
                               recognize this and do a float multiplication
   cos(6.28)   result: 0.99?   this is obvious...
   50 * 0.99?  result: 49.??   ...so is this...
   49.?? +160  result: 209.??  ...and this
   x = 209.??  result: x=210   uprounding x

and now the code that should work:
  
   x = ((double) 50 * cos((double)theta/360*6.28)) + 160;

hth

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019