Java Small example: print an approximate circle, requiring the size of a given radius to change

Source: Internet
Author: User

For example: through the API document query math class method, print the following approximate circle, as long as given a different radius, the size of the circle changes.



    Import Java.lang.Math;  public class Mathdraw {/** * drawing function, input circle radius, Output circle */public static void paint (int    R) {//Assuming the center of the circle at coordinates (r,r) int x = 0;//x coordinates start int y = R * 2; The coordinates of y begin int c = 0; Middle space quantity int z = 2;  
      
           Decrements per line, and the step size is set to 2 to adjust the aspect ratio of the screen.  
               for (int i = R * 2; I >= 0; i = i-z) {//get x Value x = GetX (r, y) of the coordinates of the drawing * point;  
               First draw the Y-value on the left side of the * System.out.print (Getspace (x) + "*"); c = (r-x) *2;  
               With the center of the output space//and then draw the Y-value on the right side of the * SYSTEM.OUT.PRINTLN (Getspace (c) + "*");  
            Decreasing the Y value each time y-=z;  }/** * Get picture * position, Input circle radius and coordinate y value, output coordinate x value/public static int GetX (int   
           R, int y) {//Take the long side of the right-angled triangle int h = y-r; Find the triangle short side long double L = math.sqrt (R * r)-(H * h));  
        Taking the X value, Math.Round () returns the nearest integer return (int) math.round (r-l); /** * Draw space */public static string getspace (int i) {string  
            s = "";  
            for (int j = 0; J < i; J +) {s + = "";  
        return s;   
        The public static void main (string[] args) {//calls the drawing function, and the parameter is the radius of the circle paint (6);   }  
    }

To run the program:


Another one:

public class Circledraw {    /**      * @param args      * @author Bigboy &  nbsp;    *     public static void Main (string[] args) {        //  TODO auto-generated Method stub         //definition radius         int R
= 6;         //defines y as the ordinate of a circle, where y=y+2 is the step to set the ordinate, you can adjust the ellipse's "fat thin"         for (int y = 0; y <= 2 * r; y = y + 2) {            long x = MATH.R
Ound (R-MATH.SQRT (2 * R * y-y * y));             for (int i = 0; I <= 2 * r; i++) {   &NBSP;&NBSP;&N Bsp          //i==x refers to a vertical axis corresponding to a ordinate, i==2*r-x refers to the ordinate corresponding to the second horizontal axis                  if (i = = X | | | = = 2 * r-x) {                    system.out.print ("*");                &nbsp} else {       &
nbsp;            system.out.print ("");                &nbsp}         &
nbsp;   }             system.out.print ("\ n");


         }    &nbsp}

To run the program:




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.