Thread: Math + Code

  1. #1
    Registered User
    Join Date
    Dec 2007

    Question Math + Code

    Alright, I'm working in a simple graphics library, graphics.h, which can be downloaded here:

    I'm currently working on a simple game which required me to be able to have a center point, and rotational value (0-360) and then be able to draw a square that has sides with a length of exactly '5' around the point at the correct rotational value... how would I accomplish this? (I need the formula)

    (Separating the points is preferable... I'd like to know where each corner is)

    Thanks in advance...

  2. #2
    Registered User
    Join Date
    Mar 2007
    What are you using to write this game, openGL, openAL, directx, something else? There isn't a precise "formula" that fits every application, but you need to figure out what angle you are rotated from according to the Z and X axis (looking from the top on a 3D plot). Then rotate that much about the Y axis. For moving the box with the rotation just make the rotational value effect the box also.

  3. #3
    Registered User
    Join Date
    Dec 2007
    No no no... I'm not using Open GL or anything like that, open the graphics library in the download and take a look at it, you'll understand it in about 5 seconds... it's really really basic. I'm actually going to need to draw the square line by line, so truely I just need the points based off of the rotation... it's a mathematical formula I want...

    Length between points (5)
    Center of the square
    Desired rotational value (0-360)

    Expected result:
    Four points that when connected make a square at the proper rotation.

    Thus a rotation of '0' would yeild
    _ _

    (The top wouldn't extend over the sides, but you get the idea..

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    graphics.h is an old borland pre-standard header, I dont think many modern compilers support it.
    Double Helix STL

  5. #5
    Registered User
    Join Date
    Dec 2007

    Yes it works

    It works... I've used it... with Dev-C++ (compiler)

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    On me hyperplane

    Thumbs up

    In pseudocode you could do it something like:
    rotation = 0;
    diagonal_length = sqrt((width * width)+(height * height));
    radius = diagonal_length / 2;
    for(int i=0; i<4; i++)
        x[i] = center_x + (cos(rotation)*radius); 
        y[i] = center_y + (sin(rotation)*radius); 
    Then just draw lines between the points. You would also have to convert degrees to radians. But it should give you an idea of how it can be done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. making programms faster
    By deian in forum C Programming
    Replies: 23
    Last Post: 10-09-2004, 12:19 AM
  3. Math function code - couple probs, tips?
    By Captain Penguin in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2002, 09:54 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM