Sri Yantra Source Code
Sri Yantra Source Code
This is really horrible code but maybe it can serve as a classroom example for how not to write code! But you might pick up from this that the analytic geometry you learned in high school does have some use after all, if you count drawing pictures as useful!
This was converted from a C program that used X windows. The C program did the Newton-Raphson to get everything lined up. I just took one solution generated by the C program and hard coded it here.
--------------------------------------------------------------------------------
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
/* Sri Yantra by Jim Kukula */
public class goforit extends java.applet.Applet {
double p[][];
double scale;
int x0, y0;
double radius[];
double wall[][];
public void intersect(double x[],
double a1[],
double a2[],
double b1[],
double b2[])
{
double sa, sb;
sa = (a1[1]-a2[1])/(a1[0]-a2[0]);
sb = (b1[1]-b2[1])/(b1[0]-b2[0]);
x[0] = (a1[1] - b1[1] + sb*b1[0] - sa*a1[0])/(sb-sa);
x[1] = b1[1] + sb*(x[0] - b1[0]);
}
public void hintersect(double x[],
double h[],
double b1[],
double b2[])
{
double r[], s[];
r = new double[2];
s = new double[2];
r[0] = -10.;
r[1] = h[1];
s[0] = 10.;
s[1] = h[1];
this.intersect(x, s, r, b1, b2);
}
public void vintersect(double x[],
double b1[],
double b2[])
{
x[0] = 0;
x[1] = b1[1] - (b2[1]-b1[1])*b1[0]/(b2[0]-b1[0]);
}
public void eightfold(double part[][], double whole[][], int cnt)
{
int i;
for(i=0; i whole[i][0] = part[i][0];
whole[i][1] = part[i][1];
}
for(i=0; i whole[cnt+i][0] = part[cnt-1-i][0];
whole[cnt+i][1] = -part[cnt-1-i][1];
}
for(i=0; i whole[2*cnt+i][0] = -part[i][1];
whole[2*cnt+i][1] = part[i][0];
}
for(i=0; i whole[3*cnt+i][0] = part[cnt-1-i][1];
whole[3*cnt+i][1] = part[cnt-1-i][0];
}
for(i=0; i whole[4*cnt+i][0] = -part[i][0];
whole[4*cnt+i][1] = -part[i][1];
}
for(i=0; i whole[5*cnt+i][0] = -part[cnt-1-i][0];
whole[5*cnt+i][1] = part[cnt-1-i][1];
}
for(i=0; i whole[6*cnt+i][0] = part[i][1];
whole[6*cnt+i][1] = -part[i][0];
}
for(i=0; i whole[7*cnt+i][0] = -part[cnt-1-i][1];
whole[7*cnt+i][1] = -part[cnt-1-i][0];
}
}
public void init()
{
int i;
double wp[][];
p = new double[28][2];
wall = new double[40][2];
wp = new double[5][2];
radius = new double[4];
this.scale = 0.6;
this.x0 = 300;
this.y0 = 300;
p[22][0] = 0.;
p[22][1] = 200.0;
p[21][1] = -p[22][1]*0.280861;
p[21][0] = p[22][1]*0.959748;
p[19][0] = p[21][0];
p[19][1] = -p[21][1];
p[20][0]= 0.0;
p[20][1] = -p[22][1];
this.hintersect(p[13], p[19], p[21], p[22]);
this.hintersect(p[17], p[21], p[19], p[20]);
p[12][0] = 0.;
p[12][1] = p[19][1] + (p[22][1]-p[19][1])/2.8;
p[14][0]=0.;
p[14][1]=p[20][1] - (p[20][1]-p[21][1])/2.8;
this.hintersect(p[26], p[12], p[14], p[13]);
this.hintersect(p[16], p[26], p[22], p[21]);
p[5][0]=0.;
p[5][1]=p[21][1];
this.hintersect(p[10], p[13], p[5], p[16]);
this.vintersect(p[18], p[10], p[17]);
this.hintersect(p[25], p[18], p[5], p[16]);
p[2][0]=0.;
p[2][1]=p[10][1];
this.hintersect(p[9], p[21], p[14], p[13]);
p[24][0] = p[25][0]*1.2;
p[24][1] = p[14][1];
this.vintersect(p[4], p[24], p[9]);
this.intersect(p[15], p[4], p[24], p[20], p[19]);
this.hintersect(p[23], p[15], p[17], p[18]);
p[8][0]=0;
p[8][1] = p[23][1];
this.hintersect(p[7], p[4], p[18], p[23]);
this.intersect(p[3], p[4], p[24], p[8], p[7]);
this.hintersect(p[11], p[3], p[14], p[26]);
this.intersect(p[27], p[4], p[24], p[5], p[25]);
this.hintersect(p[1], p[27], p[7], p[8]);
radius[0] = p[22][1];
radius[1] = (4./3.)*radius[0];
radius[2] = radius[1] + (radius[1]/radius[0])*(1./2.)*(radius[1]-radius[0]);
radius[3] = radius[2] + (1./5.)*(radius[2]-radius[1]);
wp[0][0] = radius[3];
wp[0][1] = -radius[3];
wp[1][0] = wp[0][0];
wp[1][1] = p[21][1];
wp[2][0] = (13./12.) * wp[1][0];
wp[2][1] = wp[1][1];
wp[3][0] = wp[2][0];
wp[3][1] = 0.5 * wp[0][1];
wp[4][0] = 1.25 * wp[1][0];
wp[4][1] = wp[3][1];
this.eightfold(wp, wall, 5);
for(i=0; i<40; i++)
System.out.println("x=" + wall[i][0] + ", y=" + wall[i][1]);
}
public void line(Graphics g, double x1, double y1, double x2, double y2)
{
int xw1, xw2, yw1, yw2;
xw1 = this.x0 + (int)(this.scale*x1);
xw2 = this.x0 + (int)(this.scale*x2);
yw1 = this.y0 + (int)(this.scale*y1);
yw2 = this.y0 + (int)(this.scale*y2);
g.drawLine(xw1, yw1, xw2, yw2);
}
public void circle(Graphics g, double cx, double cy, double r, Color c)
{
int tcx, tcy, w, h;
Color oldc;
oldc = g.getColor();
tcx = this.x0 + (int)(this.scale*(cx-r));
tcy = this.y0 + (int)(this.scale*(cy-r));
w = (int)(this.scale*2.0*r);
h = w;
g.setColor(c);
g.fillOval(tcx, tcy, w, h);
g.setColor(Color.black);
g.drawOval(tcx, tcy, w, h);
g.setColor(oldc);
}
public void triangle(Graphics g, int base, int tip)
{
this.line(g, -p[base][0], p[base][1], p[base][0], p[base][1]);
this.line(g, -p[base][0], p[base][1], p[tip][0], p[tip][1]);
this.line(g, p[base][0], p[base][1], p[tip][0], p[tip][1]);
}
public void polygon(Graphics g, double v[][], int cnt, Color c)
{
int i;
int xi[], yi[];
Color oldc;
oldc = g.getColor();
xi = new int[cnt];
yi = new int[cnt];
for(i=0; i xi[i] = this.x0 + (int)(this.scale*v[i][0]);
yi[i] = this.y0 + (int)(this.scale*v[i][1]);
}
g.setColor(c);
g.fillPolygon(xi, yi, cnt);
g.setColor(Color.black);
g.drawPolygon(xi, yi, cnt);
g.setColor(oldc);
}
public void paint (Graphics g)
{
Color gold = new Color(255, 204, 51);
g.setColor(Color.black);
setBackground(new Color(0, 102, 102));
this.polygon(g, wall, 40, new Color(102, 0, 51));
this.circle(g, 0.0, 0.0, radius[3], gold);
this.circle(g, 0.0, 0.0, radius[2], gold);
this.circle(g, 0.0, 0.0, radius[1], gold);
this.circle(g, 0.0, 0.0, radius[0], gold);
this.circle(g, 0.0, 0.0, 4.0, Color.white);
this.triangle(g, 21, 22);
this.triangle(g, 19, 20);
this.triangle(g, 26, 14);
this.triangle(g, 25, 5);
this.triangle(g, 24, 4);
this.triangle(g, 23, 18);
this.triangle(g, 7, 8);
this.triangle(g, 11, 12);
this.triangle(g, 1, 2);
}
}
This is really horrible code but maybe it can serve as a classroom example for how not to write code! But you might pick up from this that the analytic geometry you learned in high school does have some use after all, if you count drawing pictures as useful!
This was converted from a C program that used X windows. The C program did the Newton-Raphson to get everything lined up. I just took one solution generated by the C program and hard coded it here.
--------------------------------------------------------------------------------
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
/* Sri Yantra by Jim Kukula */
public class goforit extends java.applet.Applet {
double p[][];
double scale;
int x0, y0;
double radius[];
double wall[][];
public void intersect(double x[],
double a1[],
double a2[],
double b1[],
double b2[])
{
double sa, sb;
sa = (a1[1]-a2[1])/(a1[0]-a2[0]);
sb = (b1[1]-b2[1])/(b1[0]-b2[0]);
x[0] = (a1[1] - b1[1] + sb*b1[0] - sa*a1[0])/(sb-sa);
x[1] = b1[1] + sb*(x[0] - b1[0]);
}
public void hintersect(double x[],
double h[],
double b1[],
double b2[])
{
double r[], s[];
r = new double[2];
s = new double[2];
r[0] = -10.;
r[1] = h[1];
s[0] = 10.;
s[1] = h[1];
this.intersect(x, s, r, b1, b2);
}
public void vintersect(double x[],
double b1[],
double b2[])
{
x[0] = 0;
x[1] = b1[1] - (b2[1]-b1[1])*b1[0]/(b2[0]-b1[0]);
}
public void eightfold(double part[][], double whole[][], int cnt)
{
int i;
for(i=0; i
whole[i][1] = part[i][1];
}
for(i=0; i
whole[cnt+i][1] = -part[cnt-1-i][1];
}
for(i=0; i
whole[2*cnt+i][1] = part[i][0];
}
for(i=0; i
whole[3*cnt+i][1] = part[cnt-1-i][0];
}
for(i=0; i
whole[4*cnt+i][1] = -part[i][1];
}
for(i=0; i
whole[5*cnt+i][1] = part[cnt-1-i][1];
}
for(i=0; i
whole[6*cnt+i][1] = -part[i][0];
}
for(i=0; i
whole[7*cnt+i][1] = -part[cnt-1-i][0];
}
}
public void init()
{
int i;
double wp[][];
p = new double[28][2];
wall = new double[40][2];
wp = new double[5][2];
radius = new double[4];
this.scale = 0.6;
this.x0 = 300;
this.y0 = 300;
p[22][0] = 0.;
p[22][1] = 200.0;
p[21][1] = -p[22][1]*0.280861;
p[21][0] = p[22][1]*0.959748;
p[19][0] = p[21][0];
p[19][1] = -p[21][1];
p[20][0]= 0.0;
p[20][1] = -p[22][1];
this.hintersect(p[13], p[19], p[21], p[22]);
this.hintersect(p[17], p[21], p[19], p[20]);
p[12][0] = 0.;
p[12][1] = p[19][1] + (p[22][1]-p[19][1])/2.8;
p[14][0]=0.;
p[14][1]=p[20][1] - (p[20][1]-p[21][1])/2.8;
this.hintersect(p[26], p[12], p[14], p[13]);
this.hintersect(p[16], p[26], p[22], p[21]);
p[5][0]=0.;
p[5][1]=p[21][1];
this.hintersect(p[10], p[13], p[5], p[16]);
this.vintersect(p[18], p[10], p[17]);
this.hintersect(p[25], p[18], p[5], p[16]);
p[2][0]=0.;
p[2][1]=p[10][1];
this.hintersect(p[9], p[21], p[14], p[13]);
p[24][0] = p[25][0]*1.2;
p[24][1] = p[14][1];
this.vintersect(p[4], p[24], p[9]);
this.intersect(p[15], p[4], p[24], p[20], p[19]);
this.hintersect(p[23], p[15], p[17], p[18]);
p[8][0]=0;
p[8][1] = p[23][1];
this.hintersect(p[7], p[4], p[18], p[23]);
this.intersect(p[3], p[4], p[24], p[8], p[7]);
this.hintersect(p[11], p[3], p[14], p[26]);
this.intersect(p[27], p[4], p[24], p[5], p[25]);
this.hintersect(p[1], p[27], p[7], p[8]);
radius[0] = p[22][1];
radius[1] = (4./3.)*radius[0];
radius[2] = radius[1] + (radius[1]/radius[0])*(1./2.)*(radius[1]-radius[0]);
radius[3] = radius[2] + (1./5.)*(radius[2]-radius[1]);
wp[0][0] = radius[3];
wp[0][1] = -radius[3];
wp[1][0] = wp[0][0];
wp[1][1] = p[21][1];
wp[2][0] = (13./12.) * wp[1][0];
wp[2][1] = wp[1][1];
wp[3][0] = wp[2][0];
wp[3][1] = 0.5 * wp[0][1];
wp[4][0] = 1.25 * wp[1][0];
wp[4][1] = wp[3][1];
this.eightfold(wp, wall, 5);
for(i=0; i<40; i++)
System.out.println("x=" + wall[i][0] + ", y=" + wall[i][1]);
}
public void line(Graphics g, double x1, double y1, double x2, double y2)
{
int xw1, xw2, yw1, yw2;
xw1 = this.x0 + (int)(this.scale*x1);
xw2 = this.x0 + (int)(this.scale*x2);
yw1 = this.y0 + (int)(this.scale*y1);
yw2 = this.y0 + (int)(this.scale*y2);
g.drawLine(xw1, yw1, xw2, yw2);
}
public void circle(Graphics g, double cx, double cy, double r, Color c)
{
int tcx, tcy, w, h;
Color oldc;
oldc = g.getColor();
tcx = this.x0 + (int)(this.scale*(cx-r));
tcy = this.y0 + (int)(this.scale*(cy-r));
w = (int)(this.scale*2.0*r);
h = w;
g.setColor(c);
g.fillOval(tcx, tcy, w, h);
g.setColor(Color.black);
g.drawOval(tcx, tcy, w, h);
g.setColor(oldc);
}
public void triangle(Graphics g, int base, int tip)
{
this.line(g, -p[base][0], p[base][1], p[base][0], p[base][1]);
this.line(g, -p[base][0], p[base][1], p[tip][0], p[tip][1]);
this.line(g, p[base][0], p[base][1], p[tip][0], p[tip][1]);
}
public void polygon(Graphics g, double v[][], int cnt, Color c)
{
int i;
int xi[], yi[];
Color oldc;
oldc = g.getColor();
xi = new int[cnt];
yi = new int[cnt];
for(i=0; i
yi[i] = this.y0 + (int)(this.scale*v[i][1]);
}
g.setColor(c);
g.fillPolygon(xi, yi, cnt);
g.setColor(Color.black);
g.drawPolygon(xi, yi, cnt);
g.setColor(oldc);
}
public void paint (Graphics g)
{
Color gold = new Color(255, 204, 51);
g.setColor(Color.black);
setBackground(new Color(0, 102, 102));
this.polygon(g, wall, 40, new Color(102, 0, 51));
this.circle(g, 0.0, 0.0, radius[3], gold);
this.circle(g, 0.0, 0.0, radius[2], gold);
this.circle(g, 0.0, 0.0, radius[1], gold);
this.circle(g, 0.0, 0.0, radius[0], gold);
this.circle(g, 0.0, 0.0, 4.0, Color.white);
this.triangle(g, 21, 22);
this.triangle(g, 19, 20);
this.triangle(g, 26, 14);
this.triangle(g, 25, 5);
this.triangle(g, 24, 4);
this.triangle(g, 23, 18);
this.triangle(g, 7, 8);
this.triangle(g, 11, 12);
this.triangle(g, 1, 2);
}
}
0 Comments:
Post a Comment
<< Home