Viết chương trình vẽ đa giác số cạnh nhập vào từ bàn phím

March 07, 2023
Viết chương trình nhập vào từ bàn phím số cạnh, sao số trình diễn hình đó xoay.

Bài Giải


/*
Name: Ve da giac N canh trong thu vien Graphics
Copyright: None
Author: Tran Anh
Description: https://gimi.vn
*/
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<iostream>
using namespace std;


typedef struct
{
    int x;
    int y;
}Point;

Point XacDinh(Point Tam, int R, int G)
{
    Point Kq;
    Kq.x=Tam.x+R*cos(G*M_PI/180);
    Kq.y=Tam.y+R*sin(G*M_PI/180);
    return Kq;
}


void Draw(Point Tam, int R, int sc, int gbd,int color){
    setcolor(color);
    for(int i=0;i<sc;i++){
        Point d1 = XacDinh(Tam,R,gbd+i*360/sc);
        Point d2 = XacDinh(Tam,R,gbd+(i+1)*360/sc);
        line(d1.x,d1.y,d2.x,d2.y);
    }
}

main()
{
     int sc;
    cout<<"\t- Nhap so canh: ";
    cin>>sc;
    initwindow(800,600);
    Point Tam;
    Tam.x=400;
    Tam.y=300;
    int R=150;
    int gbd=270;
    int color=15;


    //Sao nam canh khong quay
    //Draw(Tam,R,sc,gbd,color);

    //Sao nam canh xoay``````````````````````````````

     int t=0;

    while (1)
    {
        t=t+10;
        Draw(Tam,R,sc,gbd+t,color);
        delay(400);
        Draw(Tam,R,sc,gbd+t,0);
    }
    //het doan sao nam canh xoay


    getch();
}