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

图形01:画线【难度:3级】:

答案1:

namespace smile67Kata
{
   
    using System;
    public class Kata
    {
   
        public bool inside(double x, double y){
   return (int)Math.Round(x)<=99&amp;&amp;(int)Math.Round(y)<=49&amp;&amp;(int)Math.Round(x)>=0&amp;&amp;(int)Math.Round(y)>=0;}
        public void drawLine(int x1, int y1, int x2, int y2)
        {
   
          int w=Math.Abs(x2-x1) , h=Math.Abs(y2-y1) , step=Math.Max(w,h);
          double stx=(double)(x2-x1)/step , sty=(double)(y2-y1)/step;
          for (double i=0,j=x1,k=y1;i<step;i++,j+=stx,k+=sty) if (inside(j,k)) Drawing.Canvas[(int)Math.Round(j), (int)Math.Round(k)] = true;
        }
    }
}

答案2:

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.drawLine(-100, -50, 200, 80); //your function: draw Line 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 drawLine(int x1, int y1, int x2, int y2)
        {
   
            if (x1 > x2)
            {
   
                Swap(ref x1, ref x2);
                Swap(ref y1, ref y2);
            }
            var a = (double)(y1 - y2)/(x1 - x2);
            var b = y2 - a * x2;
                     
            var xStart = Math.Min(x1, x2) > 0 ? Math.Min(x1, x2) : 0;
            var xEnd = Math.Max(x1, x2) < Drawing.Width ? Math.Max(x1, x2) : Drawing.Width;
                                 
            Console.WriteLine($"f(x)={a}x+{b}");
            Console.WriteLine($"Drawing from {xStart} to {xEnd}");
            for (var x = xStart; x < xEnd; x++)
            {
   
                var y = a * x + b;                
                Console.WriteLine($"f({x})={y}");
                if (y >= 0 &amp;&amp; y < Drawing.Height)
                    Drawing.Canvas[x, (int)y] = true;
            }
        }
        
        static void Swap(ref int lhs, ref int rhs)
        {
   
            int temp = lhs;
            lhs = rhs;
            rhs = temp;
        }
    }
}

答案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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值