# Weird Squares in Random Midpoint Displacement Algorithm

**URL:** <https://forum.kirupa.com/t/weird-squares-in-random-midpoint-displacement-algorithm/324698>\
**Category:** flash\
**Created:** [September 3, 2011, 8:32pm UTC](https://forum.kirupa.com/t/weird-squares-in-random-midpoint-displacement-algorithm/324698 "2011-09-03T20:32:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![firefly431](https://avatars.discourse-cdn.com/v4/letter/f/e99b99/32.png) [@firefly431](https://forum.kirupa.com/u/firefly431)\
**Post date:** [September 3, 2011, 8:32pm UTC](https://forum.kirupa.com/t/weird-squares-in-random-midpoint-displacement-algorithm/324698/1 "2011-09-03T20:32:02Z")

</div>

Hello, I’m trying to make a plasma fractal (as a self-challenge, I made the sierpinski triangle,  
Koch snowflake, Mandelbrot set, Julia set, and dragon fractal. If you want the code,  
PM/reply.)  
Here’s the code of frame 1:

```auto
//PLASMA FRACTAL
Square.c = 256/400;
var t:Array = new Array(new Square(new Point(0,0),new Point(400,400),Math.random()*256,Math.random()*256,Math.random()*256,Math.random()*256));
function doIt(...args) {
	graphics.clear();
	var temp:Array = new Array();
	for (var i:int = 0; i < t.length; i++) {
		var tri:Square = t* as Square;
		temp=temp.concat(tri.subdivide());
	}
	t=temp;
	for (var o:int = 0; o < t.length; o++) {
		var tr:Square = t[o] as Square;
		tr.draw(graphics);
	}
}
btn.addEventListener(MouseEvent.CLICK,doIt);
graphics.clear();
t[0].draw(graphics);
/**/ //These are for easy commenting out. I put all the fractals in one place.

```

Square subdivide:

```auto

public function subdivide():Array {
			var h:uint = (h1+h2+h3+h4)/4+Math.random()*(s.x*c);
			var r1:Square = new Square(o.clone(),new Point(s.x/2,s.y/2),h1,(h1+h2)/2,(h1+h3)/2,h);
			var r2:Square = new Square(o.clone(),new Point(s.x/2,s.y/2),(h1+h2)/2,h2,h,(h2+h4)/2);
			var r3:Square = new Square(o.clone(),new Point(s.x/2,s.y/2),(h1+h3)/2,h,h3,(h3+h4)/2);
			var r4:Square = new Square(o.clone(),new Point(s.x/2,s.y/2),h,(h2+h4)/2,(h3+h4)/2,h4);
			r2.o.offset(s.x/2,0);
			r3.o.offset(0,s.y/2);
			r4.o.offset(s.x/2,s.y/2);
			var a:Array = new Array(r1,r2,r3,r4);
			return a;
		}

```

It’s making these weird square shaped things. Run it and see.  
constructor/variables:

```auto

public var o:Point;
		public var s:Point;
		public var h1:uint,h2:uint,h3:uint,h4:uint;
		public static var c:Number = 0;
		
		public function Square(st:Point=null,sz:Point=null,htl:uint=0,htr:uint=0,hbl:uint=0,hbr:uint=0) {
			if (st == null) {
				st=new Point();
			}
			if (sz == null) {
				sz=new Point();
			}
			o=st;
			s=sz;
			h1=htl;
			h2=htr;
			h3=hbl;
			h4=hbr;
		}

```
