Skip to content

Instantly share code, notes, and snippets.

@HarryR
Created December 12, 2012 03:30
Show Gist options
  • Save HarryR/4264631 to your computer and use it in GitHub Desktop.
Save HarryR/4264631 to your computer and use it in GitHub Desktop.
Sinewave
#include "SDL.h"
#include "SDL_gfxPrimitives.h"
#include "SDL_framerate.h"
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
double derp = 0;
void
draw_spiral( SDL_Surface *surface, int x1, int y1, int x2, int y2 ) {
int plot_x, plot_y;
int cent_x, cent_y;
int i = 400;
cent_x = x1 + ((x2 - x1) / 2);
cent_y = y1 + ((y2 - y1) / 2);
derp += 0.8;
while( i-- ) {
plot_x = cent_x + sin(i) * (i / 3);
plot_y = cent_y + cos(i) * (i / 3);
int C = fabs(sin((derp + i) / 20)) * 255;
filledEllipseRGBA(surface,
plot_x, plot_y,
i / 2, i / 2,
C, C, C, 255);
}
}
#define WINDOW_WIDTH 700
#define WINDOW_HEIGHT 700
int
main( int argc, char **argv ) {
int i;
SDL_Init( SDL_INIT_VIDEO );
SDL_Surface *screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( "Moar Circles", 0 );
SDL_Event event;
FPSmanager framerate;
bool is_running = true;
SDL_initFramerate(&framerate);
SDL_setFramerate(&framerate, 30);
while( is_running ) {
if( SDL_PollEvent(&event) ) {
if( event.type == SDL_QUIT || event.type == SDL_KEYDOWN ) {
is_running = false;
}
}
SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255) );
draw_spiral(screen, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
SDL_Flip( screen );
SDL_framerateDelay(&framerate);
}
SDL_Quit();
return 0;
}
@HarryR
Copy link
Author

HarryR commented Dec 12, 2012

Draws a spiral of ellipses, and colours them using a SIN() + animation.

Static

It is also useful for getting visual knowledge of other mathematical functions, e.g. logarithms:

Logn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment