Java 在屏幕上打印椭圆
下面的代码旨在生成一个看起来像椭圆的图形,但实际上并非如此。 你必须把这个项目 网格上椭圆的轮廓。圆的公式是Java 在屏幕上打印椭圆,java,Java,下面的代码旨在生成一个看起来像椭圆的图形,但实际上并非如此。 你必须把这个项目 网格上椭圆的轮廓。圆的公式是 ((x-h)/a)^2 + ((y-h)/b)^2 = 1 代码是: public class Question33 { public static void main(String[]args) { DrawMeAnEllipse(4,12,6,4); // calling the method } public static void D
((x-h)/a)^2 + ((y-h)/b)^2 = 1
代码是:
public class Question33
{
public static void main(String[]args) {
DrawMeAnEllipse(4,12,6,4); // calling the method
}
public static void DrawMeAnEllipse(int posX, int posY, int radiusA, int radiusB)
{
int xaxis = 20;
int yaxis = 20; //scanning the coordinates
for (int x=0; x<xaxis; x++) {
for (int y=0; y<yaxis; y++){
//using equation of ellipse
int a = Math.abs((posX-x)/radiusA) * ((posX-x)/radiusA);
int b = Math.abs((posY-y)/radiusB) * ((posY-y)/radiusB);
int c = Math.abs(a + b);
if ( c=1 ) { //checking if the equation is satisfied
System.out.print('#');
} else {
System.out.print(' ') ;
}
}
System.out.println();
}
}
}
公开课问题33
{
公共静态void main(字符串[]args){
DrawMeAnEllipse(4,12,6,4);//调用该方法
}
公共静态无效椭圆(int-posX、int-posY、int-radiusA、int-radius)
{
int-xaxis=20;
int yaxis=20;//扫描坐标
对于(int x=0;x),有两件事我会考虑。
注意你在做整数除法(你会失去精度,可能会得到错误的结果)
if(c=1)
应该是if(c==1)
好的,我把它固定为c==1(现在它打印得有点正确,但不是完全正确,例如,它缺少了几个应该打印的点,但是指令要求我使用整数,现在我如何转换int,以便即使牺牲精度,它也会在屏幕上打印一个椭圆形状?