【发布时间】:2021-03-02 14:59:01
【问题描述】:
给定用户圆的半径,程序应使用等式 (x^2+y^2=r^2) 打印出圆。
它有效,但我没有得到任何东西:
- 为什么圆的点这么少,有没有办法让更多的点?
- 为什么它看起来更像椭圆/椭圆?
代码:
#include <stdio.h>
#include <stdlib.h>
void display(int r){
int x,y;
// x and y representing coordinates on an imaginary cartisian plane
for(x=-r;x<=r;x++)
{
printf("\t\t\t");//some tabs to make it kinda "centered"
for(y=-r;y<=r;y++)
{
/*if the coordinate:(x,y) is a point of the equation x^2+y^2=r^2 prints the char:*, if
not leaves that space empty*/
if( x*x+y*y == r*r)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
int main(){
int r;
printf("Enter radius:");
scanf("%d",&r);
display(r);
return 0;
}
【问题讨论】:
-
“圆圈的每个字符” - 嗯?一定是错过了几何课……
-
(1) 点数只能与
r指定的一样多。 (2) 大概是因为您终端中的字符比它们的宽度高。 -
@mkrieger1 有没有办法解决第一个问题?
-
提示:用
if (x * x + y * y - r * r < 1)替换你的条件,看看会发生什么。 -
您可能想查看 brasenham 的圆光栅化算法...here you go