Pyramids using AS3 drawing API

Let's continue from where we finished last post. We have Triangle AS3.0 class up and running and we want to use it and draw some pyramids.

If you look carefully you will notice that every pyramid consist of two triangles. This is actually simplified version where only two sides are visible and front side triangle has 3 x 60 degrees angles. This is the reason why we need only top pyramid point coordinates and its height, because with some simple math we can easily find other points.

Here is class

package com.blogspot.flanture.triangles
import com.blogspot.flanture.triangles.Triangle;
import flash.display.Sprite;
import flash.geom.Point;

public class Pyramide extends Sprite
private var p:Point; // top pyramide point
private var h:Number; // pyramide height

// for pyramide we need single Point and height value
public function Pyramide(_p:Point, _h:Number)
p = _p;
h = _h;

var b:Number = Math.sqrt((4/3)*h*h);
var p1x:Number = p.x - b;
var p2x:Number = p.x + b;
var p1y:Number = Math.floor(p.y + h);
var p2y:Number = Math.floor(p.y + h);

var t1:Triangle = new Triangle(p, new Point(p1x, p1y), new Point(p2x, p2y), true);
t1.color = 0xDCF479;

var p3x:Number = p2x + 0.1 * b;
var p3y:Number = p.y + 0.7 * h;

var t2:Triangle = new Triangle(p, new Point(p2x, p2y), new Point(p3x, p3y), true);
t2.color = 0xC9EE33;

Coordinates for smaller triangle free corner are arbitrarily and it depends on how you want your pyramid to look like.

That's why you can try and change 0.1 and 0.7 values in finding p3x and p3y.

Here is the code for test example shown on image above:

import com.blogspot.flanture.triangles.Triangle;
import com.blogspot.flanture.triangles.Pyramide;

var pyr:Pyramide = new Pyramide(new Point(100, 100), 50);

var pyr2:Pyramide = new Pyramide(new Point(250, 125), 75);

var pyr3:Pyramide = new Pyramide(new Point(300, 180), 150);

Learn more about triangles and math behind them.


Post a Comment