下面的代码旨在生成一个椭圆形的图形,但事实并非如此。 你必须投影 网格上椭圆的轮廓。圆的公式是
((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();
}
}
}
最佳答案
我会考虑一些事情。
- 请注意您正在进行整数除法(您会失去精度,并且可能会得到错误的结果)
if( c =1 )
应为if(c == 1)
。
关于java - 将椭圆打印到屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20011957/