如何画一个带星号的圆?函数 [英] How to draw a circle with asterisk? function in Ruby

查看:174
本文介绍了如何画一个带星号的圆?函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby和C ++的课程,我不得不做这个问题。你必须画一个带有星号的圆圈。我在互联网上搜索了很多,但我找不到任何完整的解决方案。我想解释一下推理,这样你不仅仅是复制代码,你才能理解它是如何工作的。



让我们开始工作:

1)您需要记住,圆圈是验证公式的一组点,所以公式可以在笛卡尔平面



2)你会发现很多公式,复杂的代码等。但是如果你停下来想想圆的每个点,你会发现圆周中的每个点都由坐标(x,y)决定



3 )你想要做的是拿一个公式,把它放在一个条件,每次一个点在飞机上验证公式打印'*'所以这样你就要画圆。



4)如何通过所有的点?你会需要两个循环。第一个将在y上移动,第二个在x上移动,它将如下所示:

  r = 5 
y = r

直到y少于-r do
x = -r
,直到x> = r do
if )
print'*'
else
print''
end
x + = 1
end
print\\\

y - = 1
end

5)好的,现在你有循环通过所有的x和y取决于半径。
现在你需要公式来验证要打印的点。

我使用的是毕达哥拉斯定理:H ^ 2 = C ^ 2 + c ^ 2



![Pythagoras Circle] [1]



所以看图像我们可以说:r ^ 2 = x ^ 2 + y ^ 2现在有了这个公式你知道哪些点属于这个圈子。



6)问题是,你使用一个char一点点大画出一个完美的圆,因此您要打印接近完美圆的所有点。你必须定义两个新的半径接近用户输入的一个,然后打印在该区域内的所有点。

所以你要有一个in_radius和一个out_radius和你要做的是从in_radius的原始半径中减去一些,并且为out_radius添加一些。

  in_radius = radius  -  0.4 
out_radius = radius + 0.4

您将使用此半径用于验证属于圆的点的条件。验证子句将如下所示:

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

将最终代码合并成如下:

  r = 7 
y = r

r_in = r-0.4
r_out = r + 0.4

,直到y减少-r do
x = -r
,直到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\\\

y - = 1
end

这是它的外观:

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



我希望这是有帮助的

Ire Ezequiel J Lopez

解决方案

这是一个等效的C ++代码

  #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 {
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;
}

输出为

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


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

解决方案

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

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

这篇关于如何画一个带星号的圆?函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆