图形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&&ny>=0&&nx<100&&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&&y<50&&x>=0&&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)&&eqs2r(i,y-j-1,x,y,r)) Drawing.Canvas[i, y-j]=true;
if (inside(i,y+j)&&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