AS3 geom.Point Vs trigonometry

Not so long ago we discussed about the fastest way to find the distance between two points.

In that case, it was a challenge between Euclidean and Manhattan distance. By the way, AS3 provides geom.Point class which comes with built in methods to deal with distances, such as distance and polar. Refer to the official docs for more information.

Will these methods allow faster computation of the distance between two points? Look at this script:

package {
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	public class pointvstrig extends Sprite {
		public function pointvstrig() {
			var p1:Point=new Point(10,20);
			var p2:Point=new Point(20,30);
			var res:Number;
			var begin,end:Number;
			var i:int;
			begin=getTimer();
			for (i=0; i<1000000; i++) {
				res=pow_euclidian(p1,p2);
			}
			end=getTimer();
			trace("Euclidian with Math.pow: "+(end-begin));
			begin=getTimer();
			for (i=0; i<1000000; i++) {
				res=mul_euclidian(p1,p2);
			}
			end=getTimer();
			trace("Euclidian with multiplication: "+(end-begin));
			begin=getTimer();
			for (i=0; i<1000000; i++) {
				res=point_distance(p1,p2);
			}
			end=getTimer();
			trace("Point.distance: "+(end-begin));
		}
		private function pow_euclidian(p1:Point, p2:Point):Number {
			return Math.sqrt(Math.pow((p1.x-p2.x),2)+Math.pow((p1.y-p2.y),2));
		}
		private function mul_euclidian(p1:Point, p2:Point):Number {
			return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
		}
		private function point_distance(p1:Point, p2:Point):Number {
			return Point.distance(p1,p2);
		}
	}
}

It calculates the distance in three ways: in the Euclidian way using Math.pow method, in the Euclidian way using the good old multiplication and using Point.distance method.

After a million calculations, these are the results:

Euclidian with Math.pow: 269ms
Euclidian with multiplication: 140ms
Point.distance: 687ms

The good old multiplication, the most basic way to calculate the power, is the fastest way to return the distance between two points. Point.distance is absolutely slow.

But I wanted to give Point class another try, calculating a point at a specified angle and distance from another point (in this case the origin).

Looks at this script:

package {
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	public class pointvstrig extends Sprite {
		public function pointvstrig() {
			var distance:Number=100;
			var angle:Number=-0.5*Math.PI;
			var p:Point;
			var begin,end:Number;
			var i:int;
			begin=getTimer();
			for (i=0; i<1000000; i++) {
				p=Point.polar(distance,angle);
			}
			end=getTimer();
			trace("Moving point with Point.polar: "+(end-begin));
			trace(p);
			begin=getTimer();
			for (i=0; i<1000000; i++) {
				p=new Point(distance*Math.cos(angle),distance*Math.sin(angle));
			}
			end=getTimer();
			trace("Moving point with trigonometry: "+(end-begin));
			trace(p);
		}
	}
}

It calculates the point in two ways: using trigonometry and using Point.polar method. This time it's not just a matter of speed, I also want a good approximation of the result that should be as close as possible to (x=0, y=-100).

After a million iterations, these are the results:

Moving point with Point.polar: 705ms
(x=6.123031769111886e-15, y=-100)
Moving point with trigonometry: 536ms
(x=6.123031769111886e-15, y=-100)

As you can see, Point class is slower again, while the results are identical.

In the end, we can say trigonometry is faster than Point class.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers