Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="ctx" width="400" height="400"></canvas>

              
            
!

CSS

              
                
              
            
!

JS

              
                ;(()=>{
  
	"use strict";
	const POINT_NUM = 20;
  const FILLL_COLORS = ["#8AB3BB","#82A5AD","#8AB4BD","#92C1CB","#7CADB6","#97CBD5","#82A5AC","#8AB4BD"]
	/**
	 * キャンバスオブジェクト
	 * @param {string} canvasId canvasのid属性
	 */
	class Canvas{
		constructor(canvasId) {
			var canvas  = document.getElementById(canvasId);
			this.ctx    = canvas.getContext('2d');
			this.canvasW = canvas.width;
			this.canvasH = canvas.height;
		}
		clear(){
			this.ctx.clearRect(0, 0, this.canvasW , this.canvasH );
		}
	}
	/**
	 * 座標オブジェクト
	 * @param {number} x X座標
	 * @param {number} y Y座標
	 */
	class Point{
		constructor(x,y) {
			this.x = x;
			this.y = y;
		}
		isEqual(point) {
			return (this.x === point.x && this.y === point.y);
		}
	}
	/**
	 * 辺オブジェクト
	 * @param {Point} start 開始座標
	 * @param {Point} end 終了座標
	 */
	class Edge{
		constructor(start,end) {
			this.start = start;
			this.end   = end;
		}
		isEqual(edge) {
			return (
				(this.start.isEqual(edge.start) && this.end.isEqual(edge.end)) ||
				(this.start.isEqual(edge.end)   && this.end.isEqual(edge.start))
			);
		}
	}
	/**
	 * 多角形オブジェクト
	 * @param {Array} points 座標情報
	 */
	class Polygon{
		constructor(points) {
			this.points = points;
		}
		draw(ctx,strokeStyle,fillStyle){
			ctx.beginPath();
			ctx.moveTo(this.points[0].x, this.points[0].y);
			for(var i=1; i<this.points.length ; i++){
				ctx.lineTo(this.points[i].x, this.points[i].y);
			}
			ctx.closePath();
			if(strokeStyle){
				ctx.strokeStyle = strokeStyle;
				ctx.stroke();
			}
			if(fillStyle){
				ctx.fillStyle   = fillStyle;
				ctx.fill();
			}
		}
	}
	/**
	 * 三角形オブジェクト
	 * @param {Array} points 座標情報
	 */
	class Triangle extends Polygon{
		/**
		* 外接円の取得
		*/
		getCircumscribedCircle() {
			//未定義の場合は定義
			if(!this._circumscribedCircle){
				var x1 = this.points[0].x;
				var y1 = this.points[0].y;
				var x2 = this.points[1].x;
				var y2 = this.points[1].y;
				var x3 = this.points[2].x;
				var y3 = this.points[2].y;
				
				//各座標の2乗を取得
				var x1pow2 = Math.pow(x1,2);
				var x2pow2 = Math.pow(x2,2);
				var x3pow2 = Math.pow(x3,2);
				var y1pow2 = Math.pow(y1,2);
				var y2pow2 = Math.pow(y2,2);
				var y3pow2 = Math.pow(y3,2);
				
				//公式より中心座標を取得
				var c = 2 * ((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
				var x = ((y3 - y1) * (x2pow2 - x1pow2 + y2pow2 - y1pow2) + (y1 - y2) * (x3pow2 - x1pow2 + y3pow2 - y1pow2)) / c;
				var y = ((x1 - x3) * (x2pow2 - x1pow2 + y2pow2 - y1pow2) + (x2 - x1) * (x3pow2 - x1pow2 + y3pow2 - y1pow2)) / c;
				
				//外接円の半径rは、中心から三角形の任意の頂点(x1,y1)までの距離
				var r = Math.sqrt(Math.pow(x1 - x,2) + Math.pow(y1 - y,2));
				this._circumscribedCircle =  new Circle(new Point(x,y),r);
			}
			return this._circumscribedCircle;
		}
		/**
		* 辺の取得
		*/
		getEdges(){
			//未定義の場合は定義
			if(!this._edges){
				this._edges = [];
				this._edges.push(new Edge(this.points[0],this.points[1]));
				this._edges.push(new Edge(this.points[1],this.points[2]));
				this._edges.push(new Edge(this.points[2],this.points[0]));
			}
			return this._edges;
		}
	}
/**
 * 円オブジェクト
 * @param {Point} center 中心座標情報
 * @param {number} radius 半径
 */
class Circle{
	constructor(center,radius){
		this.center = center;
		this.radius = radius;
	}
	/**
	 * 円内に引数の点が含まれているか確認
	 */
	isInclude(point) {
		var x = point.x - this.center.x;
		var y = point.y - this.center.y;
		var len = Math.sqrt((x * x) + (y * y));
		return len < this.radius;
	}
	/**
	* 描画
	*/
	draw(ctx,strokeStyle,fillStyle){
		ctx.beginPath();
		ctx.arc(this.center.x,this.center.y,this.radius,0,Math.PI*2,true);
		if(strokeStyle){
			ctx.strokeStyle = strokeStyle;
			ctx.stroke();
		}
		if(fillStyle){
			ctx.fillStyle   = fillStyle;
			ctx.fill();
		}
	}
};
	//表示処理
	var canvas = new Canvas("ctx");
	canvas.clear();

  var stage = {
    x : 0,
    y : 0,
    w : 400,
    h : 400
  }

	//ステージを描画
	//stage.obj = new Polygon([
	//	new Point(stage.x         , stage.y        ) , 
	//	new Point(stage.x+stage.w , stage.y        ) , 
	//	new Point(stage.x+stage.w , stage.y+stage.h) , 
	//	new Point(stage.x         , stage.y+stage.h) 
	//]);
	//stage.obj.draw(canvas.ctx,"gray");

	//ステージ内にランダムなポイントを取得
	var trianglePoints = [];
	for (var i = 0; i < POINT_NUM ; i++) {
		trianglePoints.push(new Point(
			stage.x+Math.random() * stage.w , 
			stage.y+Math.random() * stage.h
		));
	}
	trianglePoints.push(new Point(stage.x         , stage.y        )); 
	trianglePoints.push(new Point(stage.x+stage.w , stage.y        )); 
	trianglePoints.push(new Point(stage.x+stage.w , stage.y+stage.h)); 
	trianglePoints.push(new Point(stage.x         , stage.y+stage.h));
  //ランダムなポイントを描画
	//trianglePoints.forEach((point)=>{
	//	(new Circle(point,2)).draw(canvas.ctx,"red");
	//});

	//ステージを包む外接円
	var stageCircumscribedCircleRadius = Math.sqrt((stage.w*stage.w) + (stage.h*stage.h))/2;
	var stageCircumscribedCircleCenter = new Point(stage.x+stage.w/2,stage.y+stage.h/2);
	// (new Circle(stageCircumscribedCircleCenter,stageCircumscribedCircleRadius)).draw(canvas.ctx,"green");

	var circumscribedTrianglePoints = [],
		circumscribedTriangles = [];
	//ステージを包む外接円を包む外接三角形の頂点を取得
	circumscribedTrianglePoints.push(new Point(
		stageCircumscribedCircleCenter.x- Math.sqrt(3) * stageCircumscribedCircleRadius,
		stageCircumscribedCircleCenter.y-stageCircumscribedCircleRadius
	));
	circumscribedTrianglePoints.push(new Point(
		stageCircumscribedCircleCenter.x+Math.sqrt(3) * stageCircumscribedCircleRadius,
		stageCircumscribedCircleCenter.y-stageCircumscribedCircleRadius
	));
	circumscribedTrianglePoints.push(new Point(
		stageCircumscribedCircleCenter.x,
		stageCircumscribedCircleCenter.y+stageCircumscribedCircleRadius*2
	));
	//ステージを包む外接円を包む外接三角形を作成
	circumscribedTriangles.push(new Triangle(circumscribedTrianglePoints));
	//ステージを包む外接円を包む外接三角形を描画
	// circumscribedTriangles[0].draw(canvas.ctx,"yellow");

/**
* ドロネー分割
* @return {Array} ドロネー分割された三角形リスト
*/
var delaunayDiagram = (points,baseTriangle) => {
	//外部三角形を格納
	var triangles = baseTriangle
	//ドロネー分割
	points.forEach(function(point){
		var edges = [];
		var unique_edges = [];
		//ポイントを外接円に含む三角形を抽出、辺に分解
		triangles.forEach(function(targetTriangle,i){
			if(targetTriangle.getCircumscribedCircle().isInclude(point)){
				edges = edges.concat(targetTriangle.getEdges());
				delete triangles[i];
			}
		});
		//分解した辺リストから重複する辺を削除
		edges.forEach(function(edge0,i){
			var unique_flag =true;
			edges.forEach(function(edge1,j){
				//重複する辺がある場合
				if (i != j && edge0.isEqual(edge1)) {
					unique_flag= false;
				}
			});
			if(unique_flag){
				unique_edges.push(edge0)
			}
		});
		edges = unique_edges;
		//重複しない辺リストから三角形を生成
		edges.forEach(function(edge,i){
			triangles.push(new Triangle([edge.start, edge.end, point]));
		});
	});
	return triangles;
}
var triangles = delaunayDiagram(trianglePoints,circumscribedTriangles);
triangles.forEach((triangle,i)=>{
	triangle.draw(canvas.ctx,"gray",FILLL_COLORS[Math.ceil(i%FILLL_COLORS.length)]);
});
})();
              
            
!
999px

Console