Code - Lissajous

Lissajous figure

數學上,利薩茹曲線是兩個沿著互相垂直方向的正弦振動的合成的軌跡。

O_o

/*
        Lissajou Curves:
        sometimes also known as Lissajous figures or Bowditch curves.
        are pretty shapes first investigated by Nathaniel Bowditch in 1815,
        and later (and in much more detail) by Jules Antoine Lissajou in 1857.
        Liassajou curves are described by the parametric equations below:

        x = A*sin(theta*p) , y = B*sin(theta*q+phase)
        A,B is amplitude
        phase = PI / 2*p [ 0 <= phase <= PI / 2*p ]
*/

#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "raylib.h"

typedef struct {
  int x, y;
  float angle;
  float FreqA, FreqB;
  float p, q;
  float Phase;
  float AmpA, AmpB;
} Lissajous;

Lissajous lj;
void CalcAxis() {
  lj.x = (int)(lj.AmpA * sin(lj.FreqA));
  lj.y = (int)(lj.AmpB * sin(lj.FreqB + lj.Phase));
}

void PlotLissajous() {
  Color col = {255, 128, 200, 255};
  CalcAxis();
  DrawCircle(lj.x + 400, lj.y + 300, 2, col);
  float freq = lj.angle * PI / 180;
  lj.FreqA = freq * lj.p;
  lj.FreqB = freq * lj.q;
  lj.Phase = PI / (2 * lj.p);
  lj.angle += 0.1;
  if (lj.angle >= 360) {
    lj.angle = 0;
  }
}

void initLissajous() {
  lj.x = 0;
  lj.y = 0;
  lj.angle = 0;
  lj.FreqA = 0;
  lj.FreqB = 0;
  lj.p = 5;
  lj.q = 9;
  lj.Phase = PI / 2;
  lj.AmpA = 200;
  lj.AmpB = 150;
}

int main(int argc, char *argv[]) {
  const int screenWidth = 800;
  const int screenHeight = 600;
  initLissajous();
  InitWindow(screenWidth, screenHeight, "Julia Set");

  SetTargetFPS(60);  // Set our game to run at 60 frames-per-second
  int cl = 0;
  // Main game loop
  while (!WindowShouldClose())  // Detect window close button or ESC key
  {
    // Update
    //----------------------------------------------------------------------------------
    // TODO: Update your variables here
    //----------------------------------------------------------------------------------

    // Draw
    //---------------------------------------------------------------------------------
    BeginDrawing();
    if (cl == 0) {
      ClearBackground(BLACK);
      cl = 1;
    }
    PlotLissajous();
    EndDrawing();
    //----------------------------------------------------------------------------------
  }

  // De-Initialization
  //--------------------------------------------------------------------------------------
  CloseWindow();  // Close window and OpenGL context
  //--------------------------------------------------------------------------------------
  return 0;
}

See also