I've tried hard to solve this but I still couldn't make it right.
Here's what I've made.

#include <graphics.h>
#include <ctype.h>
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <process.h>

int check(int,char);
void Initialize(void);void drawEllipse1(int color);
void drawEllipse1(int color);
void drawEllipse2(int color);
    void main() {
      int color, color1;
      char ch;

       Initialize();
       ch = 'R';color1=1;color=1;
       do {
    if (ch == 'R' || ch == 'L') {
      if (ch == 'L') {
        if (color1 == 4) color = 1;
        else color = ++color1;
      }
      drawEllipse1(color);
      color1 = color;
      color = check(color, ch);
      drawEllipse2(color);
    }
    ch = toupper(getche());
       } while (ch!='X');
       closegraph();
    }

    void Initialize(void) {
      int driver = DETECT,mode, errorcode;
      initgraph(&driver,&mode, "c:\\tc\\bgi");
      errorcode = graphresult();
      if (errorcode != grOk) {
         printf("Graphics error: %s\n", grapherrormsg(errorcode));
         printf("<Copy egavga.bgi to c:\\tc\\bgi>");
         getch();
         exit(1);
      }
    }

    void drawEllipse1(int color) {
       setfillstyle(SOLID_FILL,color);
       setcolor(color);
       fillellipse((getmaxx()/2),(getmaxy()/2), 25, 50);
    }


    void drawEllipse2(int color) {
       setfillstyle(SOLID_FILL,color);
       setcolor(color);
       fillellipse(((getmaxx()/2)),(getmaxy() / 2),50,25);
    }



    int check(int color,char ch) {
     if (ch == 'R') {
       if (color < 4)
          ++color;
       else if (color == 4)
          color = 1;
     }
     if (ch == 'L') {
       if (color == 4)
         color = 1;
       else color++;
     }
     return color;
    }

I just couldn't make it rotate in 10 degrees. Please help me !

Dani AI

Generated

tried both fillellipse and the BGI ellipse(..., stangle, endangle, xradius, yradius) form; as noted, those BGI primitives are axis‑aligned only and do not accept a rotation parameter. To rotate an ellipse the straightforward, portable approach is to generate the ellipse points parametrically and apply a 2×2 rotation matrix to each point before drawing or filling.

Math and algorithm (center = (cx,cy), radii a (x‑axis) and b (y‑axis), rotation θ in degrees):

  • parametric ellipse: x = acos(t), y = bsin(t) for t in [0, 2π].
  • rotate by θ: X = xcosθ - ysinθ; Y = xsinθ + ycosθ.
  • translate to screen: screenX = cx + X; screenY = cy + Y.
  • sample t in small steps (e.g., dt = 2π / max(360, 2π*max(a,b))) and connect successive points with line() to avoid gaps.
  • to fill: collect the sampled points in an integer polygon and call fillpoly(...) if available, or draw the outline with line() and use floodfill(cx, cy, fillcolor) (ensure the polygon is closed and sampling is dense enough to avoid leaks).

Example C/C++ sketch for BGI (approximate, minimal; adapt to compiler/library):

// draw rotated ellipse centered at (cx,cy) with radii a,b rotated by deg
void drawRotatedEllipse(int cx,int cy,int a,int b,double deg,int color)
{
    double th = deg * M_PI / 180.0;
    double cs = cos(th), sn = sin(th);
    int steps = max(360, (int)(2*M_PI*max(a,b)));
    double dt = 2*M_PI / steps;
    int px = cx + (int)(a*cs + 0.5), py = cy + (int)(a*sn + 0.5);
    setcolor(color);
    for(double t = dt; t <= 2*M_PI + 1e-9; t += dt) {
        double x = a * cos(t), y = b * sin(t);
        int X = cx + (int)(x*cs - y*sn + 0.5);
        int Y = cy + (int)(x*sn + y*cs + 0.5);
        line(px, py, X, Y);
        px = X; py = Y;
    }
}

Notes and troubleshooting:

  • For a 10° step simply call the routine with deg = 10.0 (or add/subtract 10 for each keypress).
  • If the outline leaks when filling, increase steps or use fillpoly with the sampled vertex list.
  • Precompute cos/sin for performance if rotating repeatedly. This method works regardless of whether the mathematical center is (0,0) or the screen center—just set cx,cy appropriately.

This answers the rotation need without relying on BGI internals and will let the existing keypress logic increment rotation by 10° each press.

Recommended Answers

All 3 Replies

If I'm not mistaken, fillellipse draws standard (unrotated) ellipse, and there's nothing you can do to rotate it. You could try to draw your own ellipse, and then do something with that...

Indeed, That's what I want to say. I've also tried "void far ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);" function. I mean ellipse(x,y,a,b,r,n); But it just can't help and then I try to search more, there's nothing. What should I do. I should complete this before tomorow. Do you have a function that can handle it?

Hmm....

code tags, there a nice thing
[code=c] [/code]

Also this looks like C to me not C++
Could just be me but i'm quite sure its not C++

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.