Silverlight 2 Beta 1 C# Truchet Tiling

Posted by Dennis on Mar 4, 2008 in Silverlight4 comments
Truchet Tiling

Truchet Tiling is an easy way of creating random patterns. See what it looks like here.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Truchet3
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            drawTiles(40, Colors.Black);
        }

        private void drawTiles(int numTilesHorizontal, Color color)
        {
            SolidColorBrush stroke = new SolidColorBrush();
            stroke.Color = Colors.Black;

            int tileSize = (int)(Width / (double)numTilesHorizontal);
            int numTilesVertical = (int)Math.Ceiling(Height / (double)tileSize);
            int seed = 0;

            for (int i = 0; i < numTilesHorizontal + 1; i++)
            {
                for (int j = 0; j < numTilesVertical + 1; j++)
                {
                    int x = i * tileSize;
                    int y = j * tileSize;

                    // -- create a time dependend seed
                    DateTime dateTime = new DateTime(1000);
                    dateTime = DateTime.Now;
                    seed += (int)(dateTime.Millisecond);
                    Random random = new Random(seed);
                    int randomNumber = random.Next(0, 2);

                    drawTile1(x, y, tileSize, randomNumber, color);
                }
            }
        }

        private void drawTile1(int x, int y, int tileSize, int randomNumber, Color color)
        {
            double ellipseSize = (double)tileSize;
            double ellipseRadius = ellipseSize * .5;

            double leftPos1 = 0 - ellipseRadius;
            double topPos1 = (randomNumber == 0) ? 0 - ellipseRadius : tileSize - ellipseRadius;

            double leftPos2 = tileSize - ellipseRadius;
            double topPos2 = (randomNumber == 0) ? tileSize - ellipseRadius : 0 - ellipseRadius;

            Canvas canvas = new Canvas();
            canvas.Width = canvas.Height = tileSize;
            canvas.SetValue(Canvas.LeftProperty, x);
            canvas.SetValue(Canvas.TopProperty, y);

            RectangleGeometry rectMask = new RectangleGeometry();
            Rect rect = new Rect();
            rect.Width = rect.Height = tileSize;
            rectMask.Rect = rect;
            canvas.Clip = rectMask;

            SolidColorBrush stroke1 = new SolidColorBrush();
            stroke1.Color = color;

            Ellipse ellipse1 = new Ellipse();
            ellipse1.Width = ellipse1.Height = ellipseSize;
            ellipse1.StrokeThickness = .5;
            ellipse1.Stroke = stroke1;
            ellipse1.SetValue(Canvas.LeftProperty, leftPos1);
            ellipse1.SetValue(Canvas.TopProperty, topPos1);

            canvas.Children.Add(ellipse1);
            Canvas layoutRoot = FindName("LayoutRoot") as Canvas;

            SolidColorBrush stroke2 = new SolidColorBrush();
            stroke2.Color = color;

            Ellipse ellipse2 = new Ellipse();
            ellipse2.Width = ellipse2.Height = ellipseSize;
            ellipse2.StrokeThickness = .5;
            ellipse2.Stroke = stroke2;
            ellipse2.SetValue(Canvas.LeftProperty, leftPos2);
            ellipse2.SetValue(Canvas.TopProperty, topPos2);

            canvas.Children.Add(ellipse2);

            layoutRoot.Children.Add(canvas);
        }
    }
}


Tags: ,


4 comments

» Comments RSS Feed
  1. Thanks for the tutorial, it is really good:) Silverlight is something I’ve always wanted to get familiar with and I think it’s a good start!

  2. Why will anyone want to create random patterns?
    What is the point here?

  3. Nice tutorial, it is very good! Thank you for your work.
    Very happy that found your site.

  4. I think using datetime object is a kind of addiction from c++ srand object…

Leave a comment