图形01:画线【难度:3级】:
答案1:
namespace smile67Kata
{
using System;
public class Kata
{
public bool inside(double x, double y){
return (int)Math.Round(x)<=99&&(int)Math.Round(y)<=49&&(int)Math.Round(x)>=0&&(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 && 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