當前位置:編程學習大全網 - 編程語言 - 用C語言編制曲線軌跡動畫 急~

用C語言編制曲線軌跡動畫 急~

TurboC 壹個正八邊形,壹個小圓,沿八邊形移動

#include <dos.h>

#include <math.h>

#include <conio.h>

#include <stdio.h>

#include <stdlib.h>

#include <graphics.h>

void polygon(int n, int x, int y, int r, int color, float arg, int fillstyle);

void draw8(int x[], int y[], int color);

void movecircle(void);

void getpoint(float *x, float *y, float k1, float b1, float k2, float b2);

float x1[9], y1[9];

int direction = 1;

float argstep = 1;

float px, py;

float stararg = 337.5;

int x, y;

int r;

int main()

{

int GraphDriver;

int GraphMode;

float arg = 292.5;

int a, b;

int x2, y2, x3, y3;

int n = 8;

long delay;

float degree;

GraphDriver = DETECT;

initgraph(&GraphDriver, &GraphMode, "");

x = 300;

y = 200;

r = 80;

degree = atan(1) / 45;

polygon(n, x, y, r, 12, arg, 0);

px = x1[1];

py = y1[1];

px = x + r * cos(atan(1) / 45 * stararg);

py = y + r * sin(atan(1) / 45 * stararg);

circle(px, py, 8);

while(1)

{

for (delay = 0; delay < 2000000; delay++)

;

setcolor(0);

circle(px, py, 8);

draw8(x1, y1, 12);

stararg += argstep;

movecircle();

circle(px, py, 8);

while(kbhit())

{

a = getch();

if (a == 27)

{

closegraph();

return 0;

}

if (a == 0)

{

b = getch();

}

}

}

}

void polygon(int n, int x, int y, int r, int color, float arg, int fillstyle)

{

double pi;

int i;

setcolor(color);

pi = atan(1) * 4;

arg = atan(1) / 45 * arg;

x1[1] = x + r * cos(2 * pi / n + arg);

y1[1] = y + r * sin(2 * pi / n + arg);

moveto(x1[1], y1[1]);

for (i = 2; i <= n; i++)

{

x1[i] = x + r * cos(2 * pi * i / n + arg);

y1[i] = y + r * sin(2 * pi * i / n + arg);

lineto(x1[i], y1[i]);

}

lineto(x1[1], y1[1]);

if (fillstyle != 0)

{

setfillstyle(SOLID_FILL, color);

floodfill(x, y, color);

}

}

void draw8(int x[], int y[], int color)

{

int i;

setcolor(color);

moveto(x1[1], y1[1]);

for (i = 2; i <= 8; i++)

{

x1[i];

y1[i];

lineto(x1[i], y1[i]);

}

lineto(x1[1], y1[1]);

}

void getpoint(float *x, float *y, float k1, float b1, float k2, float b2)

{

*x = (b1 - b2) / (k2 - k1);

*y = k1 * *x + b1;

}

void movecircle(void)

{

float pointx, pointy;

stararg += argstep;

if (stararg > 360)

stararg -= 360;

if (stararg < 0)

stararg = -stararg;

if (stararg >= 337.5 || stararg <= 22.5)

{

px = x1[1];

py = -tan(atan(1) / 45 * stararg) * (x - px) + y;

}

else if (stararg > 22.5 && stararg <= 67.5)

{

getpoint(&pointx, &pointy,

(y1[2] - y1[3]) / (x1[2] - x1[3]),

(y1[2] - y1[3]) / (x1[2] - x1[3]) * (-x1[2]) + y1[2],

tan(atan(1) / 45 * stararg),

y - x * tan(atan(1) / 45 * stararg));

px = pointx;

py = pointy;

}

else if (stararg > 67.5 && stararg <= 112.5)

{

py = y1[3];

if (stararg == 90)

{

px = x;

}

else

{

px = x - (y - py) / (tan(atan(1) / 45 * stararg));

}

}

else if (stararg > 112.5 && stararg <= 157.5)

{

getpoint(&pointx, &pointy,

(y1[4] - y1[5]) / (x1[4] - x1[5]),

(y1[4] - y1[5]) / (x1[4] - x1[5]) * (-x1[4]) + y1[4],

tan(atan(1) / 45 * stararg),

y - x * tan(atan(1) / 45 * stararg));

px = pointx;

py = pointy;

}

else if (stararg > 157.5 && stararg <= 202.5)

{

px = x1[5];

py = -tan(atan(1) / 45 * stararg) * (x - px) + y;

}

else if (stararg > 202.5 && stararg <= 247.5)

{

getpoint(&pointx, &pointy,

(y1[6] - y1[7]) / (x1[6] - x1[7]),

(y1[6] - y1[7]) / (x1[6] - x1[7]) * (-x1[6]) + y1[6],

tan(atan(1) / 45 * stararg),

y - x * tan(atan(1) / 45 * stararg));

px = pointx;

py = pointy;

}

else if (stararg > 247.5 && stararg <= 292.5)

{

py = y1[7];

if (stararg == 90)

{

px = x;

}

else

{

px = x - (y - py) / (tan(atan(1) / 45 * stararg));

}

}

else if (stararg > 292.5 && stararg <= 337.5)

{

getpoint(&pointx, &pointy,

(y1[8] - y1[1]) / (x1[8] - x1[1]),

(y1[8] - y1[1]) / (x1[8] - x1[1]) * (-x1[8]) + y1[8],

tan(atan(1) / 45 * stararg),

y - x * tan(atan(1) / 45 * stararg));

px = pointx;

py = pointy;

}

circle(px, py, 8);

}

  • 上一篇:學電腦的話選哪個專業好?
  • 下一篇:《放飛靈魂》的作文怎麽寫?
  • copyright 2024編程學習大全網