简体   繁体   English

使用字符打印椭圆

[英]Print ellipse using characters

The general equation for a rotated ellipse centered at (h, k) has the form A(x − h)^2 + B(x − h)(y − k) + C(y − k)^2 = 1, where A and C are positive, and B^2 − 4AC < 0. 以(h,k)为中心的旋转椭圆的一般方程式为A(x-h)^ 2 + B(x-h)(y-k)+ C(y-k)^ 2 = 1 A和C为正,B ^ 2-4 AC <0。

I'm trying to print out a filled ellipse using this formula, but it only prints out *s on the first line and \\n's for the next 39 lines, where the loop breaks. 我正在尝试使用此公式打印出一个填充的椭圆,但是它仅在第一行上打印出* s,在接下来的39行上打印出\\ n,在该处循环中断。 I dont get why this is happening. 我不明白为什么会这样。

Here are my lines of code, I'm using the input A=0.04, B=0.001, C=0.01, h and k =6. 这是我的代码行,我使用输入A = 0.04,B = 0.001,C = 0.01,h和k = 6。 This should print a 5x10 ellipse, with a center 6, 6. 这应该打印一个5x10的椭圆,中心为6、6。

int x=0, y=0, h, k;
float A, B, C;

printf("input A ");
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 your code, converted into an MCVE ( Minimal, Complete, Verifiable Example ), produces the more-or-less elliptical output shown when I run it: 您的代码琐碎修正,转换为MCVE( 最小,完整,可验证的示例 ),生成的椭圆输出几乎与我运行时所示的一样:

#include <stdio.h>

int main(void)
{
    float A = 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 a number of blank lines). (加上一些空白行)。

Since the bulk of the work was done in comments and not by me, I've made this answer Community Wiki. 由于大部分工作都是在评论中完成的,而不是我自己完成的,因此我做出了这个答复,即社区Wiki。

This should work too. 这也应该起作用。 The factor 'C' is not needed really. 确实不需要因子“ C”。

int main()
{
    double A = 20;
    double B = 15;
    double h = 0;//x-offset
    double k = 0; //y-offset

    for (double y = -B - k; y < B; y++)
    {
        for (double x = -A - h; x < A; x++)
        {
            if ((x*x*B*B + y*y*A*A) < A*A*B*B)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM