How to draw a circle with asterisk? function in Ruby [closed]

こ雲淡風輕ζ 提交于 2020-01-17 16:03:19

问题


I'm taking a course in Ruby and in C++ as well and I had to do this problem. You have to draw a circle with asterisk. I searched a lot in the Internet but I couldn't find any complete solution. I want to explain the reasoning so in this way you are not just copying the code, you are going to be able to understand how it works.

Let's start to work:
1) You need to have in mind that a circle is a group of points that validate a formula, so which formula can draw a circle in a Cartesian plane?

2) You are going to find a lot of formulas, complicated code and so. But if you stop and think about each point of the circle, you are going to find out that each point in circumference is determined by to coordinates (x,y)

3) What you want to do is to take a formula, put it in a condition and each time that a point on the plane validate the formula print '*' so in that way you are going to draw the circle.

4) How to go through all the point? well you are going to need two loops. The first one is going to move on the "y" and the second one on "x" it's going to look like this:

    r = 5
    y = r

    until y less -r do
        x = -r
        until x >= r do
        if (validation condition)
            print '*'
        else
            print ' '
        end
        x += 1
    end
    print "\n"
    y -= 1
    end

5) Ok, now you have the loop to go through all the "x" and "y" depending on the radius. Now you need the formula to validate the points that you want to print.
What I used was Pythagoras theorem: H^2 = C^2 + c^2

![Pythagoras Circle][1]

So looking at the image we can say: r^2 = x^2 + y^2 now with this formula you know which points belong to the circle.

6) The problem is that you are using a char a little bit big to draw a prefect circle, so you want to print all the points that are close to the perfect circle. You have to define two new radius that are close to the one entered by the user and then print all the points that are inside that area.
So you are going to have an in_radius and an out_radius and what you going to do is just subtract some from the original radius for the in_radius and you are going to add some for the out_radius.

    in_radius = radius - 0.4
    out_radius = radius + 0.4

You are going to use this radius for the condition to validate the points that belong to the circle. The validation clause would look like this:

    if (x^2 + y^2 >= in_radius)&&(x^2 + y^2 less= out_radius)
        print '*'
    else
        print ' '
    end

Putting all together your final code would look like this:

    r = 7
    y = r

    r_in = r-0.4
    r_out = r+0.4

    until y less -r do
        x = -r
        until x >= r_out do
            if (x*x + y*y >= r_in*r_in)&&(x*x + y*y less= r_out*r_out)
                print '*'
            else
                print ' '
            end
        x += 0.5
        end
    print "\n"
    y -= 1
    end

This is how it's looks:

              *********          
          ***           ***      
        **                 **    
      **                     **  
     **                       ** 
    **                         **
    *                           *
    *                           *
    *                           *
    **                         **
     **                       ** 
      **                     **  
        **                 **    
          ***           ***      
              *********          

I hope this was helpful
Ire Ezequiel J Lopez


回答1:


Here is an equivalent C++ code

#include <iostream>


int main() 
{
    double r = 7.0;
    double r_in  = r - 0.4;
    double r_out = r + 0.4;

    for ( double y = r; y >= -r; --y )
    {
        for ( double x = -r; x < r_out; x += 0.5 )
        {
            double value = x * x + y * y; 
            if ( value >= r_in * r_in && value <= r_out * r_out )
            {
                std::cout << '*';
            }
            else
            {
                std::cout << ' ';
            }
        }
        std::cout << std::endl;
    }

    return 0;
}

The output is

          *********          
      ***           ***      
    **                 **    
  **                     **  
 **                       ** 
**                         **
*                           *
*                           *
*                           *
**                         **
 **                       ** 
  **                     **  
    **                 **    
      ***           ***      
          *********          


来源:https://stackoverflow.com/questions/24356723/how-to-draw-a-circle-with-asterisk-function-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!