Skip to content

Instantly share code, notes, and snippets.

@smallgeek
Created December 8, 2012 10:06
Show Gist options
  • Select an option

  • Save smallgeek/4239678 to your computer and use it in GitHub Desktop.

Select an option

Save smallgeek/4239678 to your computer and use it in GitHub Desktop.
KochView
using System;
using Android.Content;
using Android.Graphics;
using Android.Views;
using TurtleGraphics.Library;
namespace TurtleGraphics
{
public class KochView : View
{
private int nth;
private const int MaxNth = 5;
public KochView(Context context) : base (context)
{
Initialize();
}
private void Initialize()
{
this.nth = 1;
}
public override bool OnTouchEvent(MotionEvent e)
{
base.OnTouchEvent(e);
if (e.Action == MotionEventActions.Down)
{
this.nth++;
if (this.nth > MaxNth)
{
this.nth = 1;
}
base.Invalidate();
}
return true;
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
this.DrawBackground(canvas);
this.DrawNumber(canvas);
this.DrawKochCurve(canvas);
}
private void DrawBackground(Canvas canvas)
{
canvas.DrawColor(Color.White);
}
private void DrawNumber(Canvas canvas)
{
canvas.DrawText(nth.ToString(), 15, 15, new Paint { Color = Color.Black });
}
private void DrawKochCurve(Canvas canvas)
{
var startX = 0;
var startY = this.Height / 2;
var maxWidth = this.Width;
var action = new Action<int, int, int, int>(
(x1, y1, x2, y2) => canvas.DrawLine(x1, y1, x2, y2, new Paint { Color = Color.CornflowerBlue }));
KochCurve.Navigate(this.nth, startX, startY, maxWidth, action);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment