Need help with code snippet using trig.

BlitzMax Forums/BlitzMax Programming/Need help with code snippet using trig.

I downloaded this code from the net that draws a circle using the sine and cosine functions, but I can't figure out what I'm doing wrong in Max.

Code I converted to Max:
Global SCREEN_W:Int = 800
Global SCREEN_H:Int = 600
Graphics SCREEN_W, SCREEN_H, 0


Local x:Int, y:Int
Local length:Int = 50
Local angle:Float = 0.0
Local angle_stepsize:Float = 0.1;


SetColor 255, 255, 255
Cls
'go through all angles from 0 To 2 * Pi radians
While (angle < 2 * Pi)

    ' calculate x, y from a vector with known length and angle
    x = length * Cos (angle)
    y = length * Sin (angle)
	Plot((x + (SCREEN_W / 2)), (y + (SCREEN_H / 2)))
	angle:+angle_stepsize
	
Wend
Flip

WaitKey




And here is the code written in C using Allegro:

/*
    CIRCLE 1
    Written by Amarillion (amarillion@...)

    This program demonstrates the use of sin and cos with floats.
    All it does is draw a circle on the screen.
*/

#include <allegro.h>
#include <math.h>

// Make sure PI is defined.
// MinGW has some problems with this.
#ifndef PI
#define PI 3.1415927
#endif

void draw_circle ()
{
    int x, y;
    int length = 50;
    float angle = 0.0;
    float angle_stepsize = 0.1;

    // go through all angles from 0 to 2 * PI radians
    while (angle < 2 * PI)
    {
        // calculate x, y from a vector with known length and angle
        x = length * cos (angle);
        y = length * sin (angle);

        putpixel (screen,
            x + SCREEN_W / 2, y + SCREEN_H / 2,
            makecol (255, 255, 255));
        angle += angle_stepsize;
    }
}

int main ()
{
    // initialize Allegro
    if (allegro_init () < 0)
    {
        allegro_message ("Error: Could not initialize Allegro");
        return -1;
    }
    // initialize gfx mode
    if (set_gfx_mode (GFX_AUTODETECT, 320, 200, 0, 0) < 0)
    {
        allegro_message ("Error: Could not set graphics mode");
        return -1;
    }
    // initialize keyboard
    install_keyboard ();
    clear_keybuf ();

    // call the example function
    draw_circle ();

    // wait for a user key-press
    readkey ();

    // exit Allegro
    allegro_exit ();

    return 0;

} END_OF_MAIN ();



Different example, although I believe you're looking for the same thing:


Const Radius:Int = 100
Const Step_Angle:Float = 2.0
Const ScreenWidth:Int = 800
Const ScreenHeight:Int = 600

Global Angle:Int = 0

Graphics ScreenWidth,ScreenHeight

SetColor 255, 255, 255

Cls

While angle < 360
	
	Local X:Int = (ScreenWidth * 0.5) + (Radius * Sin(angle))
	Local Y:Int = (ScreenHeight * 0.5) + (Radius * Cos(angle))
	Plot(x,y)
	Angle:+Step_Angle
Wend

	Flip
WaitKey


Edit: If this is your first time getting into small trig, you're probobly just about to hit a time in coding that starts to really get fun, especially when messing around with fractal stuff. And even understanding this small bit will get you through a LOT of 3d formulas. Good luck.

Remember that BlitzMax works in Degrees, and your example expects Radians... :-)

@Retimer
Yeah, I'm really looking forward to using math routines instead of writing hacks.
I figure its about time. I wish I would have started 10-15 years ago though.


@Brucey
Ah...thanks! It's working now!

And just as an aside, what you're doing there has nothing to do with trigonometry.

@Rob Farley
I thought the sine and cosine functions were trigonometry functions.

I thought the sine and cosine functions were trigonometry functions.
http://en.wikipedia.org/wiki/Trigonometry
win.

I'm not sure who's winning what but Sine and Cosine functions are used in Trigonometry, however, Sine and Cosine functions do not equal trigonometry.

sine and cosine are two of the three primary trigonometric functions. This thread (and the code) are entirely about trigonometry.

That said, I've been coding a bunch of vector object transformations this way and I really enjoyed stick-building the math code and re-aquainting myself with basic high-school and undergrad math.

-pmc

And trigonometry deals with triangles. And how do you draw a circle on a computer screen? By calculating lots and lots of triangles. One for each pixel in fact.

Each pixel in a circle is one point of a right triangle, where the point's radius is the length of the hypoteneuse, the point's X coordinate is the length of the opposite side, and the point's Y coordinate is the length of the adjacent side.

Trigonometry! Ta da!