Thread: My new project

  1. #16
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define pi 3.14159265
    
    struct pos_s{
        int x;
        int y;
        };
    int main(){
        float angle;
        struct pos_s pos;
        int i;
        scanf("%d", &pos.x);
        scanf("%d", &pos.y);
        printf("processeing...\n");
        angle = ((atan2((0 + pos.y), (0 + pos.x)))*(180/pi));
        printf("final angle:%f", angle);
        return 0;
        }
    OK, but I work without a sign, and this gives the correct answer(haven't check it yet but I think it is correct....)

    Compile this code and tell me what you think...
    Last edited by yann; 09-30-2009 at 10:02 AM.
    Arduino rocks!

  2. #17
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    It appears to work OK from my testing, but I would change it to inform you it's wanting your input. I also prefer seeing stuff in radian measure than degrees, but that's just me.

  3. #18
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    I prefer degrees output, but I prefer radians for the actual calculations.

  4. #19
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    But it doesn't work when y is negative, why? It does work if x is negative, but not y...
    Arduino rocks!

  5. #20
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    I think something is wrong with the quadrants...
    Arduino rocks!

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by yann View Post
    I think something is wrong with the quadrants...
    Your point of reference for the slope can make the slope of the line, negative or positive, depending on which pair of coordinates you use as x1 y1, and which pair you use for x2 y2.

    One pair puts you at the bottom of the line, and makes the line in your diagram positive (heading into the first quadrant).

    If you change which point is x1 y1, then your point of reference changes, and now you're at the top of the line, and your slope will be negative (heading into the third quadrant). Your angle will always be positive, whatever the slope might be.

  7. #22
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Quote Originally Posted by Adak View Post
    Your point of reference for the slope can make the slope of the line, negative or positive, depending on which pair of coordinates you use as x1 y1, and which pair you use for x2 y2.

    One pair puts you at the bottom of the line, and makes the line in your diagram positive (heading into the first quadrant).

    If you change which point is x1 y1, then your point of reference changes, and now you're at the top of the line, and your slope will be negative (heading into the third quadrant). Your angle will always be positive, whatever the slope might be.
    I know, but the problem is that my angle is negative , compile my code, try it you're self...
    Arduino rocks!

  8. #23
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by yann View Post
    But it doesn't work when y is negative, why? It does work if x is negative, but not y...
    -5.0 / 2.0 = -2.5

    atan(-2.5) = -1.19028995 radians = -68.19859051 degrees


    what do you mean it doesnt work?
    Quote Originally Posted by yann View Post
    I know, but the problem is that my angle is negative , compile my code, try it you're self...

    It's supposed to be negative, thats why i said you have to check teh signs of the operators to know the quadrant it is in. if X is positive then a negative degree is in quadrant 4, if x is negative then a negative degree is in quadrant 3
    Last edited by abachler; 09-30-2009 at 02:01 PM.

  9. #24
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    crap, I should have read the entire thread.
    Last edited by NeonBlack; 09-30-2009 at 02:35 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe what he's looking for are angle numbers which are always positive, like on a compass face.

    I tend to agree. Mathematicians may use negative angle numbers, but to me, that's like driving your car with a negative steering wheel -- < huh? > doesn't work for me.

    I've added a couple lines of code to show how to tweak it for lower right quadrant, (positive x, negative y), entries:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define pi 3.14159265
    
    struct pos_s{
        int x;
        int y;
        };
    int main(void){
        float angle;
        struct pos_s pos;
        int i;
        printf("\nEnter the x coordinate: ");
        scanf("%d", &pos.x);
        printf("\nEnter the y coordinate: ");
        scanf("%d", &pos.y);
        printf("\nprocessing...\n");
        angle = ((atan2((0 + pos.y), (0 + pos.x)))*(180/pi));
        //adjustments for lower right quadrant  
        if(angle < 0)
          angle *= -1;
        if(pos.x > 0 && pos.y < 0)
          angle += 90;
    
        printf("\nfinal angle:%f", angle);
        printf("\n\n\t\t\t     press enter when ready");
        while((i = getchar()) != '\n');
        i = getchar();
        return 0;
        }
    Last edited by Adak; 09-30-2009 at 02:35 PM.

  11. #26
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
    xsign = fabs(x)/x;
    ysign = fabs(y)/y;
    
    if(xsign == 1.0 && ysign == 1.0) return angle; // quadrant 1
    
    if(xsign == -1.0) return 180.0 - angle; // quadrant 2+3
    
    if(xsign == 1.0 && ysign == -1.0) return angle + 360.0; // quadrant 4
    simple straight forward from 3rd grade math, OK from the math my dad was teaching me when i was in 3rd grade

  12. #27
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    OK, i am going to do it in 3D, I have actually done some work, its x and y for left and right and y and z for up and down, and then i am going to make it control a turret... I already have two servos
    and a micro controller(that I know how to use...I did some robotics in the 5th grade(11yrs old)...Its strange that here we have a totally different educational system...The best thing about it is that you decide what you want to be when you are about 15/17/etc...
    Arduino rocks!

  13. #28
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by yann View Post
    I already have two servos
    and a micro controller(that I know how to use...I did some robotics in the 5th grade
    So how does that work? The turret moves in two dimensions -- around the y-axis, and then around the x-axis, with one servo for each?

    Can both servos move at the same time?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #29
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    No, I actually didn't do that yet but here is what i want...The turret first adjust his angle for left and right, and the for up and down, one servo is for left and right and the other one is for up and down...I already made the program that calculates the angle for left and right and up and down, and yes, both servos can move at the same time, it will rock! I think it is gonna fire match rockets(google for it they are awsome)
    Arduino rocks!

  15. #30
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by yann View Post
    No, I actually didn't do that yet but here is what i want...The turret first adjust his angle for left and right, and the for up and down,
    Yes, that is what I meant. The left-right is around the y-axis, since the y axis is vertical. The up-down is around the x-axis, since the x axis is horizontal. You are lucky in the sense that w/ 3D graphics, moving something in both those dimensions then involves the z-axis -- if the base of the turret is 0,0,0, the end/nose of the turret will have an x, y, z value (this is where I had to learn some trigonometry*). But if you are calculating the angle for each servo independently, then you do not have to worry about that I think, in quite the same way.

    3D for real. Don't hurt anyone.

    * my hand rolled wireframe sphere still has a wicked looking (alas, unintentional) "wormhole" like structure running thru the center, pole to pole. AFAIK no one has used it to travel to another universe yet.
    Last edited by MK27; 10-01-2009 at 02:52 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM