Skip to main content
Syntax highlighting
Source Link
user12205
  • 9k
  • 3
  • 32
  • 65
function createPolygon(c,r,n){
c.width=3*r;
c.height=3*r;
var t=c.getContext("2d");
var m=c.width/2;
t.beginPath(); 
t.lineWidth="5";
t.strokeStyle="green";
var q=C(r, n);
var p=pts[0];
var a=p.X+m;
var b=p.Y+m;
t.moveTo(a,b);
for(var i=1;i<q.length;i++)
{
p=q[i];
t.lineTo(p.X+m,p.Y+m);
t.stroke();
}
t.lineTo(a,b);
t.stroke();
}
function P(x,y){
this.X=x;
this.Y=y;
}
function C(r,n){
var p=Math.PI;
var x,y,i;
var z=[];
var k=n;
var a;
for(i=0;i<k;i++)
{
a = 2*i*p/n;
x = r*Math.cos(a);
y = r*Math.sin(a);
z.push(new P(x,y));
}
return z;
}
function createPolygon(c,r,n){
c.width=3*r;
c.height=3*r;
var t=c.getContext("2d");
var m=c.width/2;
t.beginPath(); 
t.lineWidth="5";
t.strokeStyle="green";
var q=C(r, n);
var p=pts[0];
var a=p.X+m;
var b=p.Y+m;
t.moveTo(a,b);
for(var i=1;i<q.length;i++)
{
p=q[i];
t.lineTo(p.X+m,p.Y+m);
t.stroke();
}
t.lineTo(a,b);
t.stroke();
}
function P(x,y){
this.X=x;
this.Y=y;
}
function C(r,n){
var p=Math.PI;
var x,y,i;
var z=[];
var k=n;
var a;
for(i=0;i<k;i++)
{
a = 2*i*p/n;
x = r*Math.cos(a);
y = r*Math.sin(a);
z.push(new P(x,y));
}
return z;
}
function createPolygon(c,r,n) {
c.width = 3*r;
c.height = 3*r;
var ctx=c.getContext("2d");
var mid = c.width/2;
ctx.beginPath(); 
ctx.lineWidth="5";
ctx.strokeStyle="green";
var pts = ComplexRootsN(r, n);
if(null===pts || pts.length===0)
{
alert("no roots!");
return;
}
var p=pts[0];
var x0 = p.X + mid;
var y0 = p.Y + mid;
ctx.moveTo(x0,y0);
for(var i=1;i<pts.length;i++)
{
p=pts[i];
console.log(p.X +"," + p.Y);
ctx.lineTo(p.X + mid, p.Y + mid);
ctx.stroke();
}
ctx.lineTo(x0,y0);
ctx.stroke();
}

function Point(x,y){
this.X=x;
this.Y=y;
}

function ComplexRootsN(r, n){
var pi = Math.PI;
var x,y,i;
var arr = [];
var k=n;
var theta;
for(i=0;i<k;i++)
{
theta = 2*i*pi/n;
console.log('theta: ' + theta);
x = r*Math.cos(theta);
y = r*Math.sin(theta);
console.log(x+","+y);
arr.push(new Point(x,y));
}
return arr;
}
function createPolygon(c,r,n) {
c.width = 3*r;
c.height = 3*r;
var ctx=c.getContext("2d");
var mid = c.width/2;
ctx.beginPath(); 
ctx.lineWidth="5";
ctx.strokeStyle="green";
var pts = ComplexRootsN(r, n);
if(null===pts || pts.length===0)
{
alert("no roots!");
return;
}
var p=pts[0];
var x0 = p.X + mid;
var y0 = p.Y + mid;
ctx.moveTo(x0,y0);
for(var i=1;i<pts.length;i++)
{
p=pts[i];
console.log(p.X +"," + p.Y);
ctx.lineTo(p.X + mid, p.Y + mid);
ctx.stroke();
}
ctx.lineTo(x0,y0);
ctx.stroke();
}

function Point(x,y){
this.X=x;
this.Y=y;
}

function ComplexRootsN(r, n){
var pi = Math.PI;
var x,y,i;
var arr = [];
var k=n;
var theta;
for(i=0;i<k;i++)
{
theta = 2*i*pi/n;
console.log('theta: ' + theta);
x = r*Math.cos(theta);
y = r*Math.sin(theta);
console.log(x+","+y);
arr.push(new Point(x,y));
}
return arr;
}
function createPolygon(c,r,n){
c.width=3*r;
c.height=3*r;
var t=c.getContext("2d");
var m=c.width/2;
t.beginPath(); 
t.lineWidth="5";
t.strokeStyle="green";
var q=C(r, n);
var p=pts[0];
var a=p.X+m;
var b=p.Y+m;
t.moveTo(a,b);
for(var i=1;i<q.length;i++)
{
p=q[i];
t.lineTo(p.X+m,p.Y+m);
t.stroke();
}
t.lineTo(a,b);
t.stroke();
}
function P(x,y){
this.X=x;
this.Y=y;
}
function C(r,n){
var p=Math.PI;
var x,y,i;
var z=[];
var k=n;
var a;
for(i=0;i<k;i++)
{
a = 2*i*p/n;
x = r*Math.cos(a);
y = r*Math.sin(a);
z.push(new P(x,y));
}
return z;
}
function createPolygon(c,r,n) {
c.width = 3*r;
c.height = 3*r;
var ctx=c.getContext("2d");
var mid = c.width/2;
ctx.beginPath(); 
ctx.lineWidth="5";
ctx.strokeStyle="green";
var pts = ComplexRootsN(r, n);
if(null===pts || pts.length===0)
{
alert("no roots!");
return;
}
var p=pts[0];
var x0 = p.X + mid;
var y0 = p.Y + mid;
ctx.moveTo(x0,y0);
for(var i=1;i<pts.length;i++)
{
p=pts[i];
console.log(p.X +"," + p.Y);
ctx.lineTo(p.X + mid, p.Y + mid);
ctx.stroke();
}
ctx.lineTo(x0,y0);
ctx.stroke();
}

function Point(x,y){
this.X=x;
this.Y=y;
}

function ComplexRootsN(r, n){
var pi = Math.PI;
var x,y,i;
var arr = [];
var k=n;
var theta;
for(i=0;i<k;i++)
{
theta = 2*i*pi/n;
console.log('theta: ' + theta);
x = r*Math.cos(theta);
y = r*Math.sin(theta);
console.log(x+","+y);
arr.push(new Point(x,y));
}
return arr;
}
function createPolygon(c,r,n){
c.width=3*r;
c.height=3*r;
var t=c.getContext("2d");
var m=c.width/2;
t.beginPath(); 
t.lineWidth="5";
t.strokeStyle="green";
var q=C(r, n);
var p=pts[0];
var a=p.X+m;
var b=p.Y+m;
t.moveTo(a,b);
for(var i=1;i<q.length;i++)
{
p=q[i];
t.lineTo(p.X+m,p.Y+m);
t.stroke();
}
t.lineTo(a,b);
t.stroke();
}
function P(x,y){
this.X=x;
this.Y=y;
}
function C(r,n){
var p=Math.PI;
var x,y,i;
var z=[];
var k=n;
var a;
for(i=0;i<k;i++)
{
a = 2*i*p/n;
x = r*Math.cos(a);
y = r*Math.sin(a);
z.push(new P(x,y));
}
return z;
}
function createPolygon(c,r,n) {
c.width = 3*r;
c.height = 3*r;
var ctx=c.getContext("2d");
var mid = c.width/2;
ctx.beginPath(); 
ctx.lineWidth="5";
ctx.strokeStyle="green";
var pts = ComplexRootsN(r, n);
if(null===pts || pts.length===0)
{
alert("no roots!");
return;
}
var p=pts[0];
var x0 = p.X + mid;
var y0 = p.Y + mid;
ctx.moveTo(x0,y0);
for(var i=1;i<pts.length;i++)
{
p=pts[i];
console.log(p.X +"," + p.Y);
ctx.lineTo(p.X + mid, p.Y + mid);
ctx.stroke();
}
ctx.lineTo(x0,y0);
ctx.stroke();
}

function Point(x,y){
this.X=x;
this.Y=y;
}

function ComplexRootsN(r, n){
var pi = Math.PI;
var x,y,i;
var arr = [];
var k=n;
var theta;
for(i=0;i<k;i++)
{
theta = 2*i*pi/n;
console.log('theta: ' + theta);
x = r*Math.cos(theta);
y = r*Math.sin(theta);
console.log(x+","+y);
arr.push(new Point(x,y));
}
return arr;
}
golfed code and image of output example
Source Link

JavaScript 867584 (867 ungolfed)

This code uses N Complex Roots of unity and translates the angles to X,Y points. Then the origin is moved to centre of the canvas.

Golfed

function createPolygon(c,r,n){
c.width=3*r;
c.height=3*r;
var t=c.getContext("2d");
var m=c.width/2;
t.beginPath(); 
t.lineWidth="5";
t.strokeStyle="green";
var q=C(r, n);
var p=pts[0];
var a=p.X+m;
var b=p.Y+m;
t.moveTo(a,b);
for(var i=1;i<q.length;i++)
{
p=q[i];
t.lineTo(p.X+m,p.Y+m);
t.stroke();
}
t.lineTo(a,b);
t.stroke();
}
function P(x,y){
this.X=x;
this.Y=y;
}
function C(r,n){
var p=Math.PI;
var x,y,i;
var z=[];
var k=n;
var a;
for(i=0;i<k;i++)
{
a = 2*i*p/n;
x = r*Math.cos(a);
y = r*Math.sin(a);
z.push(new P(x,y));
}
return z;
}

Sample output:

Output in Chrome

Ungolfed

function createPolygon(c,r,n) {
c.width = 3*r;
c.height = 3*r;
var ctx=c.getContext("2d");
var mid = c.width/2;
ctx.beginPath(); 
ctx.lineWidth="5";
ctx.strokeStyle="green";
var pts = ComplexRootsN(r, n);
if(null===pts || pts.length===0)
{
alert("no roots!");
return;
}
var p=pts[0];
var x0 = p.X + mid;
var y0 = p.Y + mid;
ctx.moveTo(x0,y0);
for(var i=1;i<pts.length;i++)
{
p=pts[i];
console.log(p.X +"," + p.Y);
ctx.lineTo(p.X + mid, p.Y + mid);
ctx.stroke();
}
ctx.lineTo(x0,y0);
ctx.stroke();
}

function Point(x,y){
this.X=x;
this.Y=y;
}

function ComplexRootsN(r, n){
var pi = Math.PI;
var x,y,i;
var arr = [];
var k=n;
var theta;
for(i=0;i<k;i++)
{
theta = 2*i*pi/n;
console.log('theta: ' + theta);
x = r*Math.cos(theta);
y = r*Math.sin(theta);
console.log(x+","+y);
arr.push(new Point(x,y));
}
return arr;
}

This code requires HTML5 canvas element, c is canvas object, r is radius and n is # of sides.

JavaScript 867

This code uses N Complex Roots of unity and translates the angles to X,Y points. Then the origin is moved to centre of the canvas.

Ungolfed

function createPolygon(c,r,n) {
c.width = 3*r;
c.height = 3*r;
var ctx=c.getContext("2d");
var mid = c.width/2;
ctx.beginPath(); 
ctx.lineWidth="5";
ctx.strokeStyle="green";
var pts = ComplexRootsN(r, n);
if(null===pts || pts.length===0)
{
alert("no roots!");
return;
}
var p=pts[0];
var x0 = p.X + mid;
var y0 = p.Y + mid;
ctx.moveTo(x0,y0);
for(var i=1;i<pts.length;i++)
{
p=pts[i];
console.log(p.X +"," + p.Y);
ctx.lineTo(p.X + mid, p.Y + mid);
ctx.stroke();
}
ctx.lineTo(x0,y0);
ctx.stroke();
}

function Point(x,y){
this.X=x;
this.Y=y;
}

function ComplexRootsN(r, n){
var pi = Math.PI;
var x,y,i;
var arr = [];
var k=n;
var theta;
for(i=0;i<k;i++)
{
theta = 2*i*pi/n;
console.log('theta: ' + theta);
x = r*Math.cos(theta);
y = r*Math.sin(theta);
console.log(x+","+y);
arr.push(new Point(x,y));
}
return arr;
}

This code requires HTML5 canvas element, c is canvas object, r is radius and n is # of sides.

JavaScript 584 (867 ungolfed)

This code uses N Complex Roots of unity and translates the angles to X,Y points. Then the origin is moved to centre of the canvas.

Golfed

function createPolygon(c,r,n){
c.width=3*r;
c.height=3*r;
var t=c.getContext("2d");
var m=c.width/2;
t.beginPath(); 
t.lineWidth="5";
t.strokeStyle="green";
var q=C(r, n);
var p=pts[0];
var a=p.X+m;
var b=p.Y+m;
t.moveTo(a,b);
for(var i=1;i<q.length;i++)
{
p=q[i];
t.lineTo(p.X+m,p.Y+m);
t.stroke();
}
t.lineTo(a,b);
t.stroke();
}
function P(x,y){
this.X=x;
this.Y=y;
}
function C(r,n){
var p=Math.PI;
var x,y,i;
var z=[];
var k=n;
var a;
for(i=0;i<k;i++)
{
a = 2*i*p/n;
x = r*Math.cos(a);
y = r*Math.sin(a);
z.push(new P(x,y));
}
return z;
}

Sample output:

Output in Chrome

Ungolfed

function createPolygon(c,r,n) {
c.width = 3*r;
c.height = 3*r;
var ctx=c.getContext("2d");
var mid = c.width/2;
ctx.beginPath(); 
ctx.lineWidth="5";
ctx.strokeStyle="green";
var pts = ComplexRootsN(r, n);
if(null===pts || pts.length===0)
{
alert("no roots!");
return;
}
var p=pts[0];
var x0 = p.X + mid;
var y0 = p.Y + mid;
ctx.moveTo(x0,y0);
for(var i=1;i<pts.length;i++)
{
p=pts[i];
console.log(p.X +"," + p.Y);
ctx.lineTo(p.X + mid, p.Y + mid);
ctx.stroke();
}
ctx.lineTo(x0,y0);
ctx.stroke();
}

function Point(x,y){
this.X=x;
this.Y=y;
}

function ComplexRootsN(r, n){
var pi = Math.PI;
var x,y,i;
var arr = [];
var k=n;
var theta;
for(i=0;i<k;i++)
{
theta = 2*i*pi/n;
console.log('theta: ' + theta);
x = r*Math.cos(theta);
y = r*Math.sin(theta);
console.log(x+","+y);
arr.push(new Point(x,y));
}
return arr;
}

This code requires HTML5 canvas element, c is canvas object, r is radius and n is # of sides.

Source Link

JavaScript 867

This code uses N Complex Roots of unity and translates the angles to X,Y points. Then the origin is moved to centre of the canvas.

Ungolfed

function createPolygon(c,r,n) {
c.width = 3*r;
c.height = 3*r;
var ctx=c.getContext("2d");
var mid = c.width/2;
ctx.beginPath(); 
ctx.lineWidth="5";
ctx.strokeStyle="green";
var pts = ComplexRootsN(r, n);
if(null===pts || pts.length===0)
{
alert("no roots!");
return;
}
var p=pts[0];
var x0 = p.X + mid;
var y0 = p.Y + mid;
ctx.moveTo(x0,y0);
for(var i=1;i<pts.length;i++)
{
p=pts[i];
console.log(p.X +"," + p.Y);
ctx.lineTo(p.X + mid, p.Y + mid);
ctx.stroke();
}
ctx.lineTo(x0,y0);
ctx.stroke();
}

function Point(x,y){
this.X=x;
this.Y=y;
}

function ComplexRootsN(r, n){
var pi = Math.PI;
var x,y,i;
var arr = [];
var k=n;
var theta;
for(i=0;i<k;i++)
{
theta = 2*i*pi/n;
console.log('theta: ' + theta);
x = r*Math.cos(theta);
y = r*Math.sin(theta);
console.log(x+","+y);
arr.push(new Point(x,y));
}
return arr;
}

This code requires HTML5 canvas element, c is canvas object, r is radius and n is # of sides.