-1

DISCLAIMER 1: English is not my native language, sorry in advance for any errors

DISCLAMER 2: i'm new to coding, there will be for sure some imprecisions and a lot of things to improve. The waitForLeftMouseButton function was already there when i created the project, should i leave it here?


Assignment was to draw a 5-pointed star using pieslice function and for cicle. Since the assignment itself was pretty boring, i decided to make it a little more challenging by making the user decide how many spikes he wanted the star to have.

That's the code

#include <cstdlib>
#include <iostream>
#include <winbgim.h>
#include <math.h>

void waitForLeftMouseClick();

void drawStar(int Xcs, int Ycs, int Rs, int Cs_R, int Cs_G, int Cs_B);

int main()
{
    initwindow(1920,1080);
    drawStar(getmaxx()/2, getmaxy()/2, getmaxx()/4, 255, 255, 0);
    waitForLeftMouseClick(); 
    closegraph();
    return 0;
}


void drawStar(int Xcs, int Ycs, int Rs, int Cs_R, int Cs_G, int Cs_B)
{
     
     long nos, alfa, alfaA, alfaB, alfaQ;
       std::cout<<" insert number of spikes \n";
       std::cin>> nos;     // nos= number of spikes
       alfa= 360/nos;
       alfaA= 360/nos;
       alfaB= 0;
       alfaQ= alfa/4;      //alfa quarters
    setcolor(COLOR(Cs_R, Cs_G, Cs_B));
    setfillstyle(SOLID_FILL,COLOR(Cs_R, Cs_G, Cs_B));
     
    for (long i=1; i<=nos; i++)
    {
        pieslice(Xcs+Rs*cos((alfaQ+(alfaA-alfa))*M_PI/180), Ycs-Rs*sin((alfaQ+(alfaA-alfa))*M_PI/180), (180+alfaB+alfaQ)-alfaQ, (180+alfaB+alfaQ)+alfaQ, Rs);
        alfaA=alfaA+alfa;
        alfaB=alfaB+alfa;
    }
}


void waitForLeftMouseClick()
{
    clearmouseclick(WM_LBUTTONDOWN);
    const int DELAY = 50;
    int x, y;
    while (!ismouseclick(WM_LBUTTONDOWN))
        delay(DELAY);
    getmouseclick(WM_LBUTTONDOWN, x, y);
}

It works well untill 24 spikes, then it starts bugging and not drawing the star correctly. image

genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

0

You are using integers and integer division for the angles. This works well enough for large angles, but is not precise enough for smaller ones. Use floating point:

     long nos; 
     double alfa, alfaA, alfaB, alfaQ;
       std::cout<<" insert number of spikes \n";
       std::cin>> nos;     // nos= number of spikes
       alfa= 360./nos; // <- note the period
       alfaA= 360./nos;
       ...
  • the problem pesists. Might it be because of the old version of the software? i'm using dev-cpp 4.9.9.2 – toggler704 Oct 27 '21 at 11:18
  • @toggler704 it seems unlikely that versions have anything to do with it. You can edit your question with the updated code to show what exactly you are trying. –  Oct 27 '21 at 11:36