Q.no 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
// Java program for implementing Mid-Point Circle Drawing Algorithm class GFG { static void midPointCircleDraw(int h_centre,int k_centre, int r) { int h = r, k = 0; // Printing the initial point // on the axes after translation System.out.print("(" + (h + h_centre) + ", " + (k + k_centre) + ")"); // When radius is zero only a single // point will be printed if (r > 0) { System.out.print("(" + (h + h_centre) + ", " + (-k + k_centre) + ")"); System.out.print("(" + (k + h_centre) + ", " + (x + k_centre) + ")"); System.out.println("(" + (-k + h_centre) + ", " + (x + k_centre) + ")"); } // Initialising the value of P int P = 1 - r; while (h > k) { y++; // Mid-point is inside or on the perimeter if (P <= 0) P = P + 2 * k + 1; // Mid-point is outside the perimeter else { x--; P = P + 2 * k - 2 * h + 1; } // All the perimeter points have already // been printed if (h < k) break; // Printing the generated point and its // reflection in the other octants after // translation System.out.print("(" + (h + h_centre) + ", " + (k + k_centre) + ")"); System.out.print("(" + (-h + h_centre) + ", " + (k + k_centre) + ")"); System.out.print("(" + (h + h_centre) + ", " + (-k + k_centre) + ")"); System.out.println("(" + (-h + h_centre) + ", " + (-k + k_centre) + ")"); // If the generated point is on the // line h = k then the perimeter points // have already been printed if (h != k) { System.out.print("(" + (k + h_centre) + ", " + (h + k_centre) + ")"); System.out.print("(" + (-k + h_centre) + ", " + (h + y_centre) + ")"); System.out.print("(" + (k + h_centre) + ", " + (-h + k_centre) + ")"); System.out.println("(" + (-k + h_centre) + ", " + (-h + k_centre) +")"); } } } // Driver code public static void main(String[] args) { // To draw a circle of radius // 3 centered at (0, 0) midPointCircleDraw(0, 0, 3); } } |