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 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();
        }   
    }   
}

最佳答案

我会考虑一些事情。

  1. 请注意您正在进行整数除法(您会失去精度,并且可能会得到错误的结果)
  2. if( c =1 ) 应为 if(c == 1)

关于java - 将椭圆打印到屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20011957/

相关文章:

java - 使用线程概念java在多个设备中并行执行

Java 数组自定义函数

java - 使用 POS 标注器计算每个词性的数量

java - Maven Surefire 插件未运行同一原始类的第二个测试类

Java 也一样,但有一个缓冲区

java - 具有连接和排序依据的查询生成器?

java - 将原始 Map 转换为 Map<Object, Object>,会有什么问题吗?

java - AlarmManager的setRepeating()不重复,setInexactRepeating()根本不起作用

java - 在 Openshift 上的 servlet 中打开资源文件

java - 在java中是否可以在运行时获取对象的声明名称?