2017-02-24 2 views
0

Ce programme imprime un cercle dans un plan cartésien. Les entrées sont: le rayon, les coordonnées du centre du cercle (cx, cy) et le caractère avec lequel nous voulons imprimer le cercle.print ascii circle + axes issue

Si les points du cercle se chevauchent avec les axes, les points ont la priorité. J'ai écrit une condition pour l'impression des axes dans la méthode drawCircle, mais l'image est déformée ...

Quelque chose m'échappe ... quelqu'un peut-il m'aider à trouver mon erreur?

Voici mon programme entier (la méthode qui a des problèmes est le dernier, drawCircle):

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

    } 
} 
} 

Voici ce que je reçois:

enter image description here

Comme vous pouvez le voir dans l'image, les 1er et 3e cercles sont corrects, mais celui qui chevauche les axes est déformé

Répondre

0

il vous manque 3 instructions continues: vérifier cette version révisée de votre méthode 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

Merci beaucoup! Un peu nouveau pour le codage, alors j'oublie parfois ce genre de choses. –

+0

de rien. Nous y avons tous été. bonne chance. – alawand

0

En fait, quand le débogage, votre onCircle méthodes obtient pour x = 0 et y = 4, cx = 3, cy = 3:

Vous ont:

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

Par conséquent

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

retours vrai

Puis:

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

Ainsi

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

renvoie également vrai

Ainsi renvoie onCircle (rayon, cx, cy, i, j) vrai pour cette coordonnée.

Et c'est pourquoi vous obtenez votre symbole dessiné. Vous devez améliorer votre algorithme!