Flash vs Silverlight: Laser Orbit Flash vs Silverlight: Grid Transition Rotator
Sep 05

This example illustrated the differences in rendering polygon using AS3 and C#. You may also reuse the Class as your drawing library. 

Blog Logo

Today, I have completed the Website Logo. The Logo is inspired from the Chinese Concept “Taiji” (太極).

In the original Taiji Logo, a black (white) dot is inside the white (black). It means that for Chinese people, they think Black and White are not necessary completely separated. Instead, they can exist together.

This concept can be applied to Flash and Silverlight as well. They have their strength and weakness and I believe they can coexist together for solving different problem.

Comparison

Flash implementation: 45 minutes 
Silverlight implementation: 45 minutes 
What’s the difference?

  • Rendering Polygon: beginFill, lineTo [AS3] vs System.Windows.Shape.Polygon [C#]

Source codes

Flash

Silverlight

Rendering Polygon: beginFill, lineTo [AS3] vs System.Windows.Shape.Polygon [C#]

AS3 does not have any native support for rendering polygon. However, it’s quite simple to achieve this using the Graphics Library.

// AS3
// You may add this function to the Class: Shape, Sprite, MovieClip
// for rendering the polygon
public function drawPolygon(corner:int, radius : Number, color : uint = 0, alpha:Number = 1) : void {
	graphics.clear();
	graphics.beginFill(color, alpha);
	graphics.moveTo(radius, 0);

	for(var i:int = 0; i < corner; i++){
		var angle:Number = 2 *  Math.PI/ corner * (i + 1);
		var lineX:Number = Math.cos(angle) * radius;
		var lineY:Number = - Math.sin(angle) * radius;
		graphics.lineTo(lineX, lineY);
	}
	graphics.endFill();
}

In C#, you can use the Polygon Class directly to render any shape you like.

// C#
// create the Polygon
public void addPolygon(int corner, double radius , Color color)  {
	Polygon polygon = new Polygon();
	polygon.Fill = new SolidColorBrush(color);
	for(int i = 0; i < corner; i++){
		double angle = 2 *  Math.PI/ corner * (i + 1);
		double x = Math.Cos(angle) * radius;
		double y = - Math.Sin(angle) * radius;
		polygon.Points.add(new Point(x, y));
	}

	// add the polygon to the stage
	LayoutRoot.Children.Add(polygon);
}

What’s More? DispatcherTimer vs Storyboard in C#

Previously, some people suggest that I may use the Storyboard to render the animations. However, to my understands, it’s not easy to simulate some mathematical calculations (like making spring effect) purely using Storyboard. Anyway, Storyboard can be used as a replacement of DispatcherTimer. But actually I can’t see any difference yet.

The DispacherTimer is a class from the System.Windows.Threading, may be it be used for some multi-threading purpose.

// C#
// Using Storyboard to simulate ENTER FRAME Event
Storyboard storyBoard = new Storyboard();
storyBoard.Duration = TimeSpan.FromMilliseconds(1000 / FPS);
storyBoard.Completed += new EventHandler(storyBoard_Completed);
void storyBoard_Completed(object sender, EventArgs e)
{
	if(conditionToKeepGoing){
		// do something
		_storyBoard.Stop();
	}
}

// Using Storyboard to simulate ENTER FRAME Event
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 1000 / FPS);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
void _timer_Tick(object sender, EventArgs e)
{
	// do something
	if(not conditionToKeepGoing){
		timer.Stop();
	}
}

Shares and Enjoy~

Did you like this post?

Subscribe here:  

7 Responses to “Flash vs Silverlight: Dancing Polygon”

  1. Martin Beeby Says:

    When you list the implimentation times. Is that the time start to finish it took to create the samples or do you exclude research.

  2. admin Says:

    HI, Martin. The time estimation included the researh as well. Actually, those examples are pretty simple and does not require too much research yet.

    However, I will usually spent some time on thinking what kind of animation to implement which does not count towards the implementation hour.

  3. Silverlight Cream for September 27, 2008 -- #380 Says:

    [...] Bill Reiss discusses Font embedding in SL2 RC0 with example code. Flash vs Silverlight: Dancing Polygon And older entry from Terence Tsang is this animated polygon demonstration (and code) Stay in the [...]

  4. Kelly Says:

    This is a very {good|beneficial|best} {point of view|stand|standpoint|viewpoint} in the {content|article}. It’s {informative|clarifying|educational|informantory} and {helpful|assistive|uesful}. {Thanks|Thank you}.

  5. Watch free full movies Says:

    Keep up the good work! Great Post!

  6. SEO Says:

    Hey I clicked on your blog by fluke on bing while searching for something completely irrelevant but I am very glad that I did, You have just earned yourself another subscriber. :)

  7. LATARSHA Says:

    Many thanks. Still another teriffic post, surely precisely why we return for your web-site constantly…

Leave a Reply