Thread: drawing a spiral

  1. #1
    Unregistered
    Guest

    Question drawing a spiral

    hey everyone, i want to write a program that will draw a spiral, and will have as many "rings" in it as the user says. im very very new to c, so im kinda confused. btw, this is NOT a hw assignment, my teacher just introduced using graphics today, but i really want to figure out how to make this program because not knowing how to do things like this really bothers me. thanks for the help.
    i think everything is right, but i just dont know what goes inside the for() loop, which needs to be what actually draws the spiral.


    /*
    *This program will draw a spiral with as many rings as the user
    *specifies
    */

    #include <stdio.h>
    #include "genlib.h"
    #include "graphics.h"



    main()
    {

    double xmax, ymax, i;
    int rings;


    InitGraphics();
    LogCommands(TRUE);
    xmax = GetWindowWidth();
    ymax = GetWindowHeight();

    printf("How many rings do you want in the spiral?:");
    scanf("%d", &rings);


    for(i=0; i<rings; i++)
    {


    }

    ExitGraphics(); /*‘hit any key’ */
    return(0);


    }

  2. #2
    Unregistered
    Guest
    thanks, ill read over that site, hopefully ill be able to get it myself.

  3. #3
    Unregistered
    Guest
    whoa, that site is crazy! i dont understand half the stuff on it. i dont care that there are 15 different types of spirals, or what a spiral is, or where the word spiral came from, i just need to know the c commands that will allow me to draw a spiral.

    how can i incorporate trig functions into my code? i tried making a variable = to cos(theta), but it said cos was undefined, and some crap about theta. is there a symbol in c for theta(the thing that looks like a 0)? thanks for the help.

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Unregistered
    how can i incorporate trig functions into my code? i tried making a variable = to cos(theta), but it said cos was undefined, and some crap about theta. is there a symbol in c for theta(the thing that looks like a 0)? thanks for the help.
    #include <math.h>
    sorry cant help you about the theta

  5. #5
    Unregistered
    Guest
    thanks for that info.

    i really cant believe that of all the people on this board, no one knows how to draw a simple spiral. id really appreciate any help.
    thanks again to anyone who can

  6. #6
    Unregistered
    Guest

    Question

    hey i think maybe you want to look for a function in C that will plot a point and call it repeatedly, but make it follow a pattern for the points along one of the trigonometric funtions, if you set it up and think about it in a polar plane and not on a coordinate plane, then I seem to remember back to one of my calc classes that you can make a spiral using one of the trig functions on a polar plane. Now all you have to do then is convert polar to coordinate (which is VERY possible, and not too hard from what i remember) and then plot the point along that.
    it sounds very difficult but i think it will prove to be simple, I do suggest reading some advanced mathematics books to follow through with it htough, it will give you a better sense of an algorythim to do it.

    -hope i helped, if not, just ignore me, it is just a thought, from what i could concieve . -- the fish.

  7. #7
    Unregistered
    Guest
    i added some more stuff, but it only draws a small line. this isreally confusing to me.
    i appreciate anyhelp.


    /*
    *This program will draw a spiral with as many rings as the user
    *specifies
    */

    #include <stdio.h>
    #include "genlib.h"
    #include "graphics.h"
    #include <math.h>

    main()
    {

    double xmax, ymax, oldx, newx, oldy, newy, ring, diameter, x, y, degrees, i;

    ring=20;


    InitGraphics();
    LogCommands(TRUE); /*print drawing cmds */
    xmax = GetWindowWidth(); /* usually 7.0 */
    ymax = GetWindowHeight(); /* usually 6.0 */
    MovePen(-20, 20);
    if (degrees == 360)
    {
    degrees = 0;
    ring = ring--;
    }
    else { degrees++;}


    for(i=0; i<ring; i++)
    {

    diameter = diameter + 0.01;
    degrees = degrees + 0.01;
    diameter=1;
    degrees=1;

    x = diameter * sin(degrees);
    y = diameter * cos(degrees);

    oldx=GetCurrentX();
    oldy=GetCurrentY();
    MovePen(x, y);
    DrawLine(oldx-x, oldy-y);
    MovePen(x, y);


    }




    ExitGraphics();
    return(0);


    }

  8. #8
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    The equation for a simple spiral in polar coordinates (r = radius, t = angle) is about as simple as you can get:

    r = t

    CRT screens operate on cartesian coordinates, of course. These are defined by:

    x = r (cos t)
    y = r (sin t)

    so the equation of a spiral in cartesian is:

    x = t (cos t)
    y = t (sin t)

    Looped,
    Code:
    for (t = 0; t < 6.28 * 5; t += 0.1}
    {
      x = t * cos(t);
      y = t * sin(t);
      putpixel(x,y);
    }

    Play around with that to get a more general spiral (with the desired number of rings, and so on.) The general spiral equation is:

    r = A + Bt

    where A and B are constants. Just plug this into the polar to cartesion formula and vary the upper limit of the loop to get any spiral you want.

    Edit: The for loop increment should be less than one.
    Last edited by Procyon; 10-11-2001 at 09:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing HBITMAP into CWnd, Acquired from "screenshot"
    By DeusAduro in forum Windows Programming
    Replies: 6
    Last Post: 07-02-2009, 03:41 PM
  2. Slow drawing code
    By tjpanda in forum Windows Programming
    Replies: 5
    Last Post: 05-09-2008, 05:09 PM
  3. Line Drawing Algorithm
    By Axpen in forum Game Programming
    Replies: 15
    Last Post: 08-01-2005, 06:30 PM
  4. How to do double buffing with win32 drawing?
    By Josh Kasten in forum Windows Programming
    Replies: 2
    Last Post: 03-27-2004, 12:02 AM
  5. drawing minimaps and radar screens.
    By Eber Kain in forum Game Programming
    Replies: 4
    Last Post: 03-08-2002, 11:44 AM