C#练习题答案: 图形02:绘制圆圈【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客提供了10种不同的方法来解决C#中绘制圆圈的编程题目,涵盖了从基础到进阶的多种解决方案,适合C#初学者和进阶者进行编程练习和面试准备。
摘要由CSDN通过智能技术生成

图形02:绘制圆圈【难度:3级】:

答案1:

namespace smile67Kata
{
   
    using System;
    
    //Some general infos - you can use following preloaded "functions/vars" from the preloaded "Drawing"- Class:
    //const int Width = 100, Height = 50; //Canvas size, here always fix, 0,0 is top,left
    //static bool[,] Canvas; //your drawing Canvas with size [Width,Height], here you have to set your pixels;-)
    //Clear() //clears Canvas for new drawing(s)
    //ShowCanvas("Y", ref Drawing.Canvas) //here "Y" is the sign for your pixel as string output
    
    //Example for drawing and showing the result - look at testcases (usable for own tests?):
    //---------------------------------------------------------------------------------------
    //Kata draw = new Kata();   //Canvas now has size 100x50, is empty and ready for drawing
    //Drawing.Clear(); //clears Canvas (every pixel=false, after new not necessary)
    //draw.drawCircle(-10,20, 40); //your function: draw Circle on Canvas and perhaps clip it (set Canvas pixels on true)
    //Console.WriteLine(Drawing.ShowCanvas("Y", ref Drawing.Canvas)); //show Canvas/Image with your line in output window (symbol for pixel="Y")
    
    public class Kata
    {
   
        //Drawing.Canvas[0, 0] = true; //example for single pixel at 0,0 = left, top corner
        public void drawCircle(int x, int y, int r)
        {
   
          double ang = 2*Math.PI*(double)r;
          for(int i=0; i<=ang; i++) {
   
            int nx=x+(int)Math.Round(r*Math.Sin(2*Math.PI*i/ang)), ny=y+(int)Math.Round(r*Math.Cos(2*Math.PI*i/ang));
            if(nx>=0&amp;&amp;ny>=0&amp;&amp;nx<100&amp;&amp;ny<50) Drawing.Canvas[nx,ny]=true;
          }
        }
    }
}

答案2:

namespace smile67Kata
{
   
    using System;
    public class Kata
    {
   
        public bool inside(int x, int y){
   return x<100&amp;&amp;y<50&amp;&amp;x>=0&amp;&amp;y>=0;} 
        public bool eqs2r(int x1,int y1,int x2,int y2,int r){
   return Math.Round(Math.Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))==r;}
        public void drawCircle(int x, int y, int r){
   
          for (int i=0;i<100;i++) {
   
            for (int j=0;j<r;j++){
   
              if (inside(i,y-j)&amp;&amp;eqs2r(i,y-j-1,x,y,r)) Drawing.Canvas[i, y-j]=true;
              if (inside(i,y+j)&amp;&amp;eqs2r(i,y+j+1,x,y,r)) Drawing.Canvas[i, y+j]=true;
            }
          }
          //Console.WriteLine(Drawing.ShowCanvas("S", ref Drawing.Canvas)); 
        }
    }
}

答案3:

namespace smile67Kata
{
   
    using System;
    //Some general infos - you can use following preloaded "functions/vars" from the preloaded "Drawing"- Class:
    //const int Width = 100, Height = 50; //Canvas size, here always fix, 0,0 is top,left
    //static bool[,] Canvas; //your drawing Canvas with size [Width,Height], here you have to set your pixels;-)
    //Clear() //clears Canvas for new drawing(s)
    //ShowCanvas("Y", ref Drawing.Canvas) //here "Y" is the sign for your pixel as string output
    
    //Example for drawing and showing the result - look at testcases (usable for own tests?):
    //---------------------------------------------------------------------------------------
    //Kata draw = new Kata();   //Canvas now has size 100x50, is empty and ready for drawing
    //Drawing.Clear(); //clears Canvas (every pixel=false, after new not necessary)
    //draw.drawCircle(-10,20, 40); //your function: draw Circle on Canvas and perhaps clip it (set Canvas pixels on true)
    //Console.WriteLine(Drawing.ShowCanvas("Y", ref Drawing.Canvas)); //show Canvas/Image with your line in output window (symbol for pixel="Y")
    
    public class Kata
    {
   
        //Drawing.Canvas[0, 0] = true; //example for single pixel at 0,0 = left, top corner
        public void drawCircle(int x, int y, int r)
        {
   
          for (double step = -r; step <= r; step += 0.05)
          {
   
            int stepX = (int)step;
            int stepY = (int)Math.Sqrt(Math
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值