# include <graphics.h>
#include <math.h>
#include <stdio.h>
#define UP 0
#define DOWN 1
#define pie -3.1415
typedef int penState;
typedef struct{float v,h;}aPoint;
typedef struct
{
float heading;
penState pen;
int color;
aPoint Pos;
}newTurtle;
newTurtle turtle;
void StartTurtleGraphics()
{
setcolor(2);
rectangle(10,10,630,470);
outtextxy(20,20,"my name:zky");
outtextxy(220,20,"expriment num:31057157");
outtextxy(420,20,"question num:0.3");
line(10,30,630,30);
}
void SetTurtle()
{
float x,y,h;
if(turtle.Pos.v>600)turtle.Pos.v=600;
if(turtle.Pos.v<40)turtle.Pos.v=40;
if(turtle.Pos.h<60)turtle.Pos.h=60;
if(turtle.Pos.h>440)turtle.Pos.h=440;
x=turtle.Pos.v;
y=turtle.Pos.h;
h=turtle.heading;
setcolor(turtle.color);
circle(x,y,20);
circle(x+10,y,8);
circle(x-10,y,8);
circle(x,y+10,8);
circle(x,y-10,8);
circle(x+25*cos(h*pie/180),y+25*sin(h*pie/180),5);
line(x-20*cos(h*pie/180),y-20*sin(h*pie/180),x-30*cos(h*pie/180),y-30*sin(h*pie/180));
}
void StarTurtle()
{
turtle.Pos.v=320;
turtle.Pos.h=250;
turtle.heading=0;
turtle.color=2;
turtle.pen=DOWN;
SetTurtle();
}
void PenUP()
{
turtle.pen=UP;
}
void PenDown()
{
turtle.pen=DOWN;
}
float TurtleHeading()
{
return turtle.heading;
}
aPoint*TurtlePos()
{
aPoint *a;
a=&turtle.Pos;
return a;
}
void Move(int steps)
{
int a=turtle.color;
float m,n;
m=turtle.Pos.v;
n=turtle.Pos.h;
turtle.color=0;
SetTurtle();
turtle.color=a;
turtle.Pos.v+=steps*cos(turtle.heading*pie/180);
turtle.Pos.h+=steps*sin(turtle.heading*pie/180);
SetTurtle();
if(turtle.pen)
line(m,n,turtle.Pos.v,turtle.Pos.h);
}
void Turn(float degress)
{
int a=turtle.color;
turtle.color=0;
SetTurtle();
turtle.heading+=degress;
while(turtle.heading>=360)turtle.heading-=360;
while(turtle.heading<=0)turtle.heading+=360;
turtle.color=a;
SetTurtle();
}
void MoveTTo(aPoint newPos)
{
int a=turtle.color;
float m,n;
m=turtle.Pos.v;
n=turtle.Pos.h;
turtle.color=0;
SetTurtle();
turtle.color=a;
turtle.Pos.v=newPos.v;
turtle.Pos.h=newPos.h;
SetTurtle();
if(turtle.pen)
line(m,n,turtle.Pos.v,turtle.Pos.h);
}
void TurnTTo(float angle)
{
turtle.heading=angle;
}
void SetTurtleColor(int color)
{
turtle.color=color;
}
void main()
{
int gd = DETECT, gm = 0;
registerbgidriver(EGAVGA_driver);
initgraph(&gd, &gm, "");
StartTurtleGraphics();
StarTurtle();
Turn(30);
Move(100);
SetTurtleColor(4);
setcolor(4);
circle(turtle.Pos.v,turtle.Pos.h,30);
}