c - Print ellipse using characters -
the general equation rotated ellipse centered @ (h, k) has form a(x − h)^2 + b(x − h)(y − k) + c(y − k)^2 = 1, , c positive, , b^2 − 4ac < 0.
i'm trying print out filled ellipse using formula, prints out *s on first line , \n's next 39 lines, loop breaks. dont why happening.
here lines of code, i'm using input a=0.04, b=0.001, c=0.01, h , k =6. should print 5x10 ellipse, center 6, 6.
int x=0, y=0, h, k; float a, b, c; printf("input "); scanf("%f", &a); printf("input b "); scanf("%f", &b); printf("input c "); scanf("%f", &c); printf("input h "); scanf("%f", &h); printf("input k "); scanf("%f", &k); while(1){ if(y>=40){ break; } if((a*((x-h)*(x-h)))+(b*(x-h)*(y-k))+(c*((y-k)*(y-k)))<=1){ printf("*"); x++; continue; } if(x>=80){ printf("\n"); y++; continue; } else{ printf(" "); x++; continue; } } return 0;
this trivial fix-up of code, converted mcve (minimal, complete, verifiable example), produces more-or-less elliptical output shown when run it:
#include <stdio.h> int main(void) { float = 0.04; float b = 0.001; float c = 0.01; int h = 6; int k = 6; int x = 0; int y = 0; while (y < 40) { if ((a * (x - h) * (x - h)) + (b * (x - h) * (y - k)) + (c * (y - k) * (y - k)) <= 1.0) { printf("*"); x++; } else if (x >= 80) { printf("\n"); y++; x = 0; } else { printf(" "); x++; } } return 0; }
sample output:
$ ./ellipse ******** ********* ********* ********* ********* ********* *********** ********* ********* ********* ********* ********* ******** ******* ****** ***** *
(plus number of blank lines).
since bulk of work done in comments , not me, i've made answer community wiki.
Comments
Post a Comment