2017-02-24 8 views
0

이 프로그램은 데카르트 평면에 원을 인쇄합니다.인쇄 ascii circle + axes 문제

입력 값은 반경, 원의 중심 좌표 (cx, cy) 및 원을 인쇄 할 문자입니다.

원의 점이 축과 겹치면 점이 우선 순위를 갖습니다. drawCircle 메서드에서 축 인쇄를위한 조건을 작성했지만 이미지가 왜곡되었습니다 ...

뭔가가 나를 뽑아 내고 있습니다 ... 누군가 내 실수를 찾도록 도와 줄 수 있습니까?

enter image description here

당신이 볼 수 있듯이 :

여기
public class Circle { 

public static void main (String[] args){ 

    System.out.println(onCircle(1,2,3,4,5)); 

    drawCircle(1,3,3,'*'); 
    drawCircle(3,3,3,'*'); 
    drawCircle(5,10,12,'*'); 

} 
//Question 1A 
public static boolean onCircle (int radius, int cx, int cy, int x, int y){ 
    //default answer is false, but if the inequality holds then it is set to true 
    boolean isDrawn = false; 
    if(Math.pow(radius,2)<=(Math.pow((x-cx),2)+Math.pow((y-cy),2)) && (Math.pow((x-cx),2)+Math.pow((y-cy),2))<=(Math.pow(radius,2)+1)){ 
     isDrawn = true; 
    } 
    return isDrawn; 
} 

//Question 1B 
public static void verifyInput (int radius, int cx, int cy){ 
    //if radius is negative, display error message 
    if (radius<=0){ 
    throw new IllegalArgumentException(" The radius of the circle must be a positive number."); 
    } 
    //if the center of the circle with radius 'radius' causes the circle to 'overflow' into other quadrants 
    //, display error message 
    if ((cx-radius)<0 || (cy-radius)<0){ 
    throw new IllegalArgumentException("the circle with requested parameters does not fit in the quadrant." 
             +"Consider moving the center of the circle further from the axes."); 
    } 
} 

//Question 1C 
public static void drawCircle (int radius, int cx, int cy, char symbol){ 
    verifyInput(radius,cx,cy); 

    //set the values for extension of the axes (aka how long are they) 
    int xMax = cx+radius+1; 
    int yMax = cy+radius+1; 

    for(int j=yMax; j>=0; j--){ 
    for(int i=0; i<=xMax; i++){ 

     //set of if-block to print the axes 
     if (i == 0 && j == 0){ 
     System.out.print('+'); 
     } 
     else if(i == 0){ 
     if (j == yMax){ 
      System.out.print('^'); 
     } 
     if(j != yMax && onCircle(radius,cx,cy,i,j)==false){ 
      System.out.print('|'); 
     } 
     } 

     else if(j == 0){ 
     if(i == xMax){ 
      System.out.print('>'); 
     } 
     if(i != xMax && onCircle(radius,cx,cy,i,j) == false){ 
      System.out.print('-'); 
     } 
     } 

     //if block to print the circle 
     //verify for each coordinate (i,j) in the quadrant if they are on circle 
     //if =true print symbol, if =false print empty character 
     if(onCircle(radius,cx,cy,i,j)==true){ 
     System.out.print(symbol); 
     } 
     else{ 
     System.out.print(' '); 
     } 

    } 
    System.out.println(); 

    } 
} 
} 

내가 얻고 무엇을 : 여기

내 전체 프로그램 (문제가 방법은 마지막, drawCircle입니다)입니다 그림에서 첫 번째와 세 번째 원은 괜찮지 만 축과 겹치는 원은 비뚤어집니다.

답변

0

누락 된 문구가 3 개 있습니다 : 체크 아웃 귀하의 drawCircle 방법의 수정 된 버전 :

사실
public static void drawCircle (int radius, int cx, int cy, char symbol){ 
    verifyInput(radius,cx,cy); 

    //set the values for extension of the axes (aka how long are they) 
    int xMax = cx+radius+1; 
    int yMax = cy+radius+1; 

for(int j=yMax; j>=0; j--){ 
    for(int i=0; i<=xMax; i++){ 

    //set of if-block to print the axes 
    if (i == 0 && j == 0){ 
    System.out.print('+'); 
    continue; 
    } 
    else if(i == 0){ 
    if (j == yMax){ 
     System.out.print('^'); 
    } 
    if(j != yMax && onCircle(radius,cx,cy,i,j)==false){ 
     System.out.print('|'); 
     continue; 
    } 
    } 

    else if(j == 0){ 
    if(i == xMax){ 
     System.out.print('>'); 
    } 
    if(i != xMax && onCircle(radius,cx,cy,i,j) == false){ 
     System.out.print('-'); 
     continue; 
    } 
    } 

    //if block to print the circle 
    //verify for each coordinate (i,j) in the quadrant if they are on circle 
    //if =true print symbol, if =false print empty character 
    if(onCircle(radius,cx,cy,i,j)==true){ 
    System.out.print(symbol); 
    } 
    else{ 
    System.out.print(' '); 
    } 

} 
System.out.println(); 

} 
} 
+0

대단히 감사합니다. 코딩에 조금 익숙하지 않기 때문에, 나는 때때로 이런 종류의 것을 잊어 버립니다. –

+0

당신을 진심으로 환영합니다. 우린 모두 거기에 있었어. 행운을 빕니다. – alawand

0

디버깅, 당신의 onCircle 방법은 X = 0, Y = 4, CX = 3, CY = 3 얻을 때 :

당신 이 : 따라서

Math.pow(radius=3,2) = 9 
Math.pow((x - cx), 2) = 9 
Math.pow((y - cy), 2) = 1 

Math.pow(radius, 2) <= Math.pow((x - cx), 2) + Math.pow((y - cy), 2)

,

복귀이어서 진정한

: 따라서

(Math.pow((x-cx),2) = 9 
Math.pow((y-cy),2)) = 1 
(Math.pow(radius,2)+1)) = 10 

또한

(Math.pow((x-cx),2)+Math.pow((y-cy),2)) <= (Math.pow(radius,2)+1)) 

복귀 진정한

따라서 onCircle (반경 CX, CY, I, J)를 반환 이 좌표의 경우입니다.

그래서 당신의 상징을 그려야합니다. 알고리즘을 개선해야합니다!