Advance Computer Graphics
Advance Computer Graphics
Source code:-
#include<stdio.h>
#include<graphics.h>
//#include<dos.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int i;
int gdriver = DETECT, gmode;
int midx, midy;
int radius = 50;
int lx,ly;
char pattern[25];
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(1);
setbkcolor(5);
getfillpattern(pattern);
setfillpattern(pattern,9);
while(!kbhit())
{
for(i=0;i<35;i++)
{
if(kbhit())
{break;}
cleardevice();
circle(midx,midy,150);
circle(midx-60,midy-60,30);
circle(midx+60,midy-60,30);
circle(midx+50+i, midy-60, 8);
fillellipse(midx+50+i, midy-60, 8, 8);
circle(midx-70+i, midy-60, 8);
fillellipse(midx-70+i, midy-60, 8, 8);
arc(midx, midy+60, 190,350, 50);
delay(50);
}
for(i=0;i<45;i++)
{
if(kbhit())
{break;}
cleardevice();
circle(midx,midy,150);
circle(midx-60,midy-60,30);
circle(midx+60,midy-60,30);
circle(midx+50+34-i, midy-60, 8);
fillellipse(midx+50+34-i, midy-60, 8, 8);
circle(midx-70+34-i, midy-60, 8);
1
fillellipse(midx-70+34-i, midy-60, 8, 8);
arc(midx, midy+60, 190,350, 50);
delay(50);
}
}
}
Output:-
2
2. Write a program to implement Translational and Rotational Sweep
Translational Sweep
Source code:-
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy,i,j;
int radius = 60;
char pattern[20];
getfillpattern(pattern);
setfillpattern(pattern,4);
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(2);
for(i=0;i<170;i++)
{
circle(midx-i, midy+i, radius);
fillellipse(midx-i, midy+i, radius, radius);
delay(10);
}
/* clean up */
getch();
closegraph();
return 0;
}
Output:-
3
Rotational Sweep
Source code:-
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
// rotate point
float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;
return p;
}
void main()
{
int x1,i,y1,x2,y2,midx,midy;
POINT a,b;
char p[20];
double deg;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
midx=getmaxx()/2;
midy=getmaxy()/2 ;
a.x=midx+100;
a.y=midy;
getfillpattern(p);
setfillpattern(p,2);
setcolor(5);
for(i=0;i<350;i++)
{
b=rotate_point(midx,midy,0.017444*i,a); //1 degree =0.017444
rad
circle(b.x,b.y,20);
4
fillellipse(b.x,b.y,20,20);
putpixel(b.x,b.y,1);
delay(10);
}
getch();
}
Output:-
5
3. Write a program To implement Polygon Mesh Representation using
A. Explicit Representation
B. Pointer to Vertex List
C. Pointer to Edge List
A. Explicit Representation
Source Code:-
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy,n,i,j,k;
point a[25],b,c,d;
setcolor(getmaxcolor());
printf("Enter no of vertex");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d].x =",i);
scanf("%d",&a[i].x);
printf("a[%d].y =",i);
scanf("%d",&a[i].y);
}
cleardevice();
for(i=0;i<n;i++)
{
if(i==n-1)
line(a[0].x,a[0].y, a[n-1].x,a[n-1].y);
else
line(a[i].x,a[i].y, a[i+1].x,a[i+1].y);
delay(1000);
}
getch();
closegraph();
return 0;
}
6
Output:-
Source Code :
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
7
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy,n,i,j,pn,totalpoly,k;
int poly[25];
polygone p[25];
point a[25],b,c,d;
setcolor(getmaxcolor());
printf("Enter no of vertex");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d].x =",i);
scanf("%d",&a[i].x);
printf("a[%d].y =",i);
scanf("%d",&a[i].y);
}
printf("\nEnter no of Polygon");
scanf("%d",&totalpoly);
for(k=0;k<totalpoly;k++)
{
}
cleardevice();
for(k=0;k<totalpoly;k++)
{
for(i=0;i<p[k].vertexno;i++)
{
if(i==p[k].vertexno-1)
line(a[p[k].vertex[0]].x,a[p[k].vertex[0]].y,
a[p[k].vertex[p[k].vertexno-1]].x,a[p[k].vertex[p[k].vertexno-
1]].y);
else
line(a[p[k].vertex[i]].x,a[p[k].vertex[i]].y,
a[p[k].vertex[i+1]].x,a[p[k].vertex[i+1]].y);
delay(1000);
}
}
getch();
closegraph();
return 0;
}
8
Output:-
Source Code:-
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
9
{
int v1,v2;
int p1,p2;
}edgelist;
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy,n,i,j,pn,totalpoly,k;
int totaledges,en;
int poly[25];
polygone p[25];
edgelist E[25];
point a[25],b,c,d;
printf("Enter no of vertex");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d].x =",i);
scanf("%d",&a[i].x);
printf("a[%d].y =",i);
scanf("%d",&a[i].y);
}
for(i=0;i<totaledges;i++)
{
printf("\n Enter index of vertex1 of %d Edge ",i);
scanf("%d",&E[i].v1);
printf("\n Enter index of vertex2 of %d Edge ",i);
scanf("%d",&E[i].v2);
printf("\n Enter index of plane1 of %d Edge ",i);
scanf("%d",&E[i].p1);
}
printf("\nEnter no of Polygon");
scanf("%d",&totalpoly);
for(k=0;k<totalpoly;k++)
{
printf("\nEnter no of edges in polygone %d",k);
scanf("%d",&p[k].edgeno);
for(j=0;j<p[k].edgeno;j++)
{
printf("Edge[%d]=",j);
scanf("%d",&p[k].edges[j]);
}
}
cleardevice();
for(k=0;k<totalpoly;k++)
10
{
for(i=0;i<p[k].edgeno;i++)
{
line(a[E[p[k].edges[i]].v1].x,a[E[p[k].edges[i]].v1].y,
a[E[p[k].edges[i]].v2].x,a[E[p[k].edges[i]].v2].y);
delay(1000);
}
}
getch();
closegraph();
return 0;
}
Output:-
11
12
4. Write a program to generate a solid from two basic solids using Constructive
Solid Geometry.
Source Code :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int i;
char *pattern;
int gd= DETECT, gm;
initgraph(&gd,&gm,"e:\\tc\\bgi");
setcolor(2);
/* draw a rectangle */
rectangle(200,30,300,130);
getfillpattern(pattern);
setfillpattern(pattern, 4);
floodfill(250,100,2);
setfillpattern(pattern, 9);
fillellipse(500,65,30,20);
delay(10000);
for(i=500;i>=250;i--)
{
cleardevice();
rectangle(200,30,300,130);
getfillpattern(pattern);
setfillpattern(pattern, 4);
floodfill(250,100,2);
setfillpattern(pattern, 9);
fillellipse(i,65,30,20);
delay(25);
}
getch();
closegraph();
return 0;
}
13
Output:-
14
5. Write a program to generate following parametric cubic curves.
a) B-spline
b) Bazier
c) Hermite
a) B-spline
Source Code :
void spline(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
float x,y;
float t;
for(t=0.00;t<=1.0;t+=0.001)
{
x=((pow(1-t,3)*x1)+((3*pow(t,3)-6*pow(t,2)+4)*x2)+((-
3*pow(t,3)+3*pow(t,2)+3*t+1)*x3)+(pow(t,3)*x4))/6;
y=((pow(1-t,3)*y1)+((3*pow(t,3)-6*pow(t,2)+4)*y2)+((-
3*pow(t,3)+3*pow(t,2)+3*t+1)*y3)+(pow(t,3)*y4))/6;
delay(5);
putpixel((int)(x),(int)(y),RED);
}
}
int main()
{
int x1,y1,x2,y2,x3,y3,x4,y4;
int gd= DETECT, gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
clrscr();
printf("\n Enter the coordinates of Control Point P1 :");
printf("\n Enter x1 : ");
scanf("%d",&x1);
printf("Enter y1 : ");
scanf("%d",&y1);
printf("\n Enter the coordinates of Control Point P2 :");
printf("\n Enter x2 : ");
scanf("%d",&x2);
printf("Enter y2 : ");
scanf("%d",&y2);
printf("\n Enter the coordinates of Control Point P3 :");
printf("\n Enter x3 : ");
scanf("%d",&x3);
15
printf("Enter y3 : ");
scanf("%d",&y3);
printf("\n Enter the coordinates of Control Point P4 :");
printf("\n Enter x4 : ");
scanf("%d",&x4);
printf("Enter y4 : ");
scanf("%d",&y4);
cleardevice();
//spline(100,100,200,300,400,300,600,200);
spline(x1,y1,x2,y2,x3,y3,x4,y4);
getch();
closegraph();
return 0;
}
Output :
b) Bazier
16
Source code :
//Beizer curve
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void bezier(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
float x,y;
float t;
for(t=0.00;t<=1.0;t+=0.001)
{
x=pow(1-t,3)*x1+(3*t*pow(1-t,2))*x2+(3*pow(t,2)*(1-
t))*x3+pow(t,3)*x4;
y=pow(1-t,3)*y1+(3*t*pow(1-t,2))*y2+(3*pow(t,2)*(1-
t))*y3+pow(t,3)*y4;
delay(5);
putpixel((int)(x),(int)(y),RED);
}}
int main()
{
int x1,y1,x2,y2,x3,y3,x4,y4;
int gd= DETECT, gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
clrscr();
printf("\n Enter the coordinates of Point P1 :");
printf("\n Enter x1 : ");
scanf("%d",&x1);
printf("Enter y1 : ");
scanf("%d",&y1);
printf("\n Enter the coordinates of Control Point P2 :");
printf("\n Enter x2 : ");
scanf("%d",&x2);
printf("Enter y2 : ");
scanf("%d",&y2);
printf("\n Enter the coordinates of Control Point P3 :");
printf("\n Enter x3 : ");
scanf("%d",&x3);
printf("Enter y3 : ");
scanf("%d",&y3);
printf("\n Enter the coordinates of Point P4 :");
17
printf("\n Enter x4 : ");
scanf("%d",&x4);
printf("Enter y4 : ");
scanf("%d",&y4);
cleardevice();
//bezier(100,100,200,300,400,300,600,400);
bezier(x1,y1,x2,y2,x3,y3,x4,y4);
getch();
closegraph();
return 0;
}
Output :
c) Hermite
Source code :
//Hermite curve
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
18
void hermite(int x1,int y1,int x4,int y4,int rx1,int ry1,int rx4,int ry4)
{
float x,y;
float t;
for(t=0.00;t<=1.0;t+=0.001)
{
x=(2*pow(t,3)-3*pow(t,2)+1)*x1+(-2*pow(t,3)+3*pow(t,2))*x4+(pow(t,3)-
2*pow(t,2)+t)*rx1+(pow(t,3)-pow(t,2))*rx4;
y=(2*pow(t,3)-3*pow(t,2)+1)*y1+(-2*pow(t,3)+3*pow(t,2))*y4+(pow(t,3)-
2*pow(t,2)+t)*ry1+(pow(t,3)-pow(t,2))*ry4;
delay(5);
putpixel((int)(x),(int)(y),RED);
}
}
int main()
{
int x1,y1,x4,y4,rx1,ry1,rx4,ry4;
int gd= DETECT, gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
clrscr();
printf("\n Enter the coordinates of Point P1 :");
printf("\n Enter x1 : ");
scanf("%d",&x1);
printf("Enter y1 : ");
scanf("%d",&y1);
printf("\n Enter the coordinates of Point P4 :");
printf("\n Enter x4 : ");
scanf("%d",&x4);
printf("Enter y4 : ");
scanf("%d",&y4);
printf("\n Enter the coordinates of Control Point R1 :");
printf("\n Enter rx1 : ");
scanf("%d",&rx1);
printf("Enter ry1 : ");
scanf("%d",&ry1);
printf("\n Enter the coordinates of Control Point R4 :");
printf("\n Enter rx4 : ");
scanf("%d",&rx4);
printf("Enter ry4 : ");
scanf("%d",&ry4);
cleardevice();
//hermite(100,120,150,30,50,50,200,200);
hermite(x1,y1,x4,y4,rx1,ry1,rx4,ry4);
getch();
closegraph();
return 0;
}
Output:
19
6. Implement following algorithms for visible surface determination.
(i) Scan line algorithm
(ii) Z-buffer algorithm
Source code :
#include<stdio.h>
20
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm, n, i, j, k,np,l,coc=2;
struct edge ed[10],temped;
float dx,dy,m[10],x_int[10],inter_x[10];
int x[10],y[10],ymax = 0, ymin = 480, yy,temp;
initgraph (&gd, &gm, "C:\\TurboC3\\BGI");
/* read no of polygons
-------------------------------*/
printf("enter the no of polygons");
scanf("%d",&np);
/* Read the vertices of the polygon and also find Ymax and Ymin
--------------------------------------------------------------- */
for(l=1;l<=np;l++)
{
printf("Enter the vertices for polygon %d: \n",l);
for(i = 0; i < n; i++)
{
printf("x[%d] : ", i);
scanf("%d", &x[i]);
printf("y[%d] : ", i);
scanf("%d", &y[i]);
if(y[i] > ymax)
ymax = y[i];
if(y[i] < ymin)
ymin = y[i];
ed[i].x1 = x[i];
ed[i].y1 = y[i];
}
21
-------------------------------*/
for(i=0;i<n-1;i++)
{
ed[i].x2 = ed[i+1].x1;
ed[i].y2 = ed[i+1].y1;
ed[i].flag=0;
}
ed[i].x2 = ed[0].x1;
ed[i].y2 = ed[0].y1;
ed[i].flag=0;
22
ed[j]=ed[j+1];
ed[j+1] = temped;
}
if(ed[j].y2==ed[j+1].y2)
{
if(ed[j].x1<ed[j+1].x1)
{
temped = ed[j];
ed[j]=ed[j+1];
ed[j+1] = temped;
} }
} }
}
/* calculating 1/slope of each edge and storing top x
coordinate of the edge --------------------------- */
for(i=0;i<n;i++)
{
dx = ed[i].x2 - ed[i].x1;
dy = ed[i].y2 - ed[i].y1;
if(dy==0)
{
m[i]=0;
}
else
{
m[i] = dx/dy;
}
inter_x[i]= ed[i].x1;
}
yy=ymax;
while(yy>ymin)
{
23
for(i=0;i<n;i++)
{
if(ed[i].flag==1)
{
if(yy==ed[i].y1)
{
x_int[j]=ed[i].x1;
j++;
if(ed[i-1].y1==yy && ed[i-1].y1<yy)
{
x_int[j]=ed[i].x1;
j++;
}
if(ed[i+1].y1==yy && ed[i+1].y1<yy)
{
x_int[j]=ed[i].x1;
j++;
}
}
else
{
x_int[j] = inter_x[i]+(-m[i]);
inter_x[i]=x_int[j];
j++;
}
}
}
24
setcolor(coc);
coc++;
}
getch();
}
Output :
Source code :
ZComposite.java
import java.awt.Composite;
import java.awt.CompositeContext;
import java.awt.RenderingHints;
25
import java.awt.RenderingHints.Key;
import java.awt.image.ColorModel;
import java.util.Arrays;
public class ZComposite implements Composite {
buffer[y*width + x] = value;
}
public double[] getBuffer() {
return buffer;
}
public void setValueResolver(ZValueResolver valueResolver) {
this.valueResolver = valueResolver;
this.valueResolver.setAntialiasingEnabled(antialiasingEnabled);
}
26
public ZValueResolver getValueResolver() {
return valueResolver;
}
public double getZOf(int x, int y) {
if (x >= width || x < 0 ||
y >= height || y < 0) {
throw new IllegalArgumentException("Point [" + x + ", " + y + "] is outside
of the Z Buffer array ["+width+", "+height+"]");
}
ZCompositeContext.java
import java.awt.CompositeContext;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
public class ZCompositeContext implements CompositeContext {
protected final static byte R_BAND = 0;
protected final static byte G_BAND = 1;
protected final static byte B_BAND = 2;
protected ZComposite zComposite;
ZCompositeContext(ZComposite zComposite) {
this.zComposite = zComposite;
}
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
ZValueResolver zValueResolver = zComposite.getValueResolver();
if (zValueResolver == null) {
throw new IllegalArgumentException("You must set a ZValueResolver before draw any
polygon with this composite");
}
int maxX = dstOut.getMinX() + dstOut.getWidth();
int maxY = dstOut.getMinY() + dstOut.getHeight();
for (int y = dstOut.getMinY(); y < maxY; y++) {
for (int x = dstOut.getMinX(); x < maxX; x++) {
int dstInX = -dstIn.getSampleModelTranslateX() + x;
int dstInY = -dstIn.getSampleModelTranslateY() + y;
double dstZ = zComposite.getZOf(dstInX, dstInY);
double srcZ = zValueResolver.resolve(dstInX, dstInY);
if (srcZ < dstZ) {
27
zComposite.setZOf(dstInX, dstInY, srcZ);
dstOut.setSample(x, y, R_BAND, src.getSample(x, y, R_BAND)); //R
dstOut.setSample(x, y, G_BAND, src.getSample(x, y, G_BAND)); //G
dstOut.setSample(x, y, B_BAND, src.getSample(x, y, B_BAND)); //B
} else if (srcZ == dstZ) {
dstOut.setSample(x, y, R_BAND, src.getSample(x, y, R_BAND)); //R
dstOut.setSample(x, y, G_BAND, src.getSample(x, y, G_BAND)); //G
dstOut.setSample(x, y, B_BAND, src.getSample(x, y, B_BAND)); //B
} else {
dstOut.setSample(x, y, R_BAND, dstIn.getSample(x, y, R_BAND)); //R
dstOut.setSample(x, y, G_BAND, dstIn.getSample(x, y, G_BAND)); //G
dstOut.setSample(x, y, B_BAND, dstIn.getSample(x, y, B_BAND)); //B
}
} } }
public void dispose() {
}
}
Zcompossitesnippet.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ZCompositeSnippet {
public static void main(String[] args) {
final JFrame frame = new JFrame("ZBuffer composite");
JPanel bufferdPanel = new JPanel() {
private static final long serialVersionUID = 1959924209249841311L;
protected void paintComponent(Graphics g) {
int[] xpoints;
int[] ypoints;
Graphics2D g2d = (Graphics2D) g;
ZComposite composite = new ZComposite(640, 480);
composite.clearBufferBit();
composite.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setComposite(composite);
xpoints = new int[] {
0, 100, 100, 0
};
ypoints = new int[] {
0, 50, 100, 50
};
28
composite.setValueResolver(ZValueResolverFactory.createPolygonResolver(xpoints, ypoints,
100, 0, 0));
g2d.setColor(Color.red);
g2d.fillPolygon(xpoints, ypoints, xpoints.length);
g2d.setColor(Color.yellow);
g2d.drawPolygon(xpoints, ypoints, xpoints.length);
xpoints = new int[] {
0, 100, 100, 0
};
ypoints = new int[] {
50, 0, 50, 100
};
composite.setValueResolver(ZValueResolverFactory.createPolygonResolver(xpoints, ypoints,
0, 100, 100));
g2d.setColor(Color.yellow);
g2d.fillPolygon(xpoints, ypoints, xpoints.length);
g2d.setColor(Color.red);
g2d.drawPolygon(xpoints, ypoints, xpoints.length);
}
};
frame.getContentPane().add(bufferdPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.getContentPane().addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
frame.setTitle(e.getX()+", " +e.getY());
} });
frame.setVisible(true);
} }
ZplaneResolver.java
import java.awt.Shape;
public class ZPlaneResolver implements ZValueResolver {
protected double[] plane;
protected boolean antialiasingEnabled;
protected Shape clippingShape;
public ZPlaneResolver() {
}
public ZPlaneResolver(double x1, double x2, double x3, double y1, double y2, double y3,
double z1, double z2, double z3) {
this(ZValueResolverFactory.createPlane(x1, x2, x3, y1, y2, y3, z1, z2, z3));
}
public ZPlaneResolver(double[] plane) {
setPlane(plane);
}
public void setPlane(double[] plane) {
if (plane.length != 4) {
throw new IllegalArgumentException("");
29
}
this.plane = plane;
}
public double resolve(double x, double y) {
if (isAntialiasingEnabled() && isNotInBounds(x, y)) {
return Double.MAX_VALUE;
}
return ZValueResolverFactory.resolveZ(x, y, plane);
}
protected boolean isNotInBounds(double x, double y) {
if (clippingShape == null) {
throw new IllegalStateException("For antialiasing you must set clipping shape");
}
if (clippingShape.contains(x, y)) {
return false;
}
return true;
}
public boolean isAntialiasingEnabled() {
return antialiasingEnabled;
}
public void setAntialiasingEnabled(boolean isEnabled) {
antialiasingEnabled = isEnabled;
}
public void setClippingShape(Shape shape) {
this.clippingShape = shape;
}
}
ZValueResolver.java
import java.awt.Shape;
public interface ZValueResolver {
double resolve(double x, double y);
boolean isAntialiasingEnabled();
void setAntialiasingEnabled(boolean isEnabled);
void setClippingShape(Shape shape);
}
ZvalueresolverFactory.java
import java.awt.geom.GeneralPath;
public class ZValueResolverFactory {
public static ZValueResolver createPolygonResolver(final int[] xpoints, final int[] ypoints,
double z1, double z2, double z3) {
GeneralPath generalPath = createClippingShape(xpoints, ypoints);
ZPlaneResolver resolver = new ZPlaneResolver(
xpoints[0], xpoints[1], xpoints[2],
ypoints[0], ypoints[1], ypoints[2],
z1, z2, z3);
30
resolver.setClippingShape(generalPath);
return resolver;
}
public static GeneralPath createClippingShape(final int[] xpoints, final int[] ypoints) {
if (xpoints.length < 3) {
throw new IllegalArgumentException("Polygon must have >2 points");
}
GeneralPath generalPath = new GeneralPath();
generalPath.moveTo(xpoints[0], ypoints[0]);
for (int i=1; i<xpoints.length; i++) {
generalPath.lineTo(xpoints[i], ypoints[i]);
}
generalPath.closePath();
return generalPath;
}
public static GeneralPath createClippingShape(final double[] xpoints, final double[]
ypoints) {
if (xpoints.length < 3) {
throw new IllegalArgumentException("Polygon must have >2 points");
}
GeneralPath generalPath = new GeneralPath();
generalPath.moveTo(xpoints[0], ypoints[0]);
for (int i=1; i<xpoints.length; i++) {
generalPath.lineTo(xpoints[i], ypoints[i]);
}
generalPath.closePath();
return generalPath;
}
public static double[] createPlane(double x1, double x2, double x3,
double y1, double y2, double y3,
double z1, double z2, double z3) {
double[] plane = new double[4];
//A
plane[0] = y1*(z2 -z3) + y2*(z3 -z1) + y3*(z1 -z2);
//B
plane[1] = z1*(x2 -x3) + z2*(x3 -x1) + z3*(x1 -x2);
//C
plane[2] = x1*(y2 -y3) + x2*(y3 -y1) + x3*(y1 -y2);
//D
plane[3] = -( x1*(y2*z3 -y3*z2) + x2*(y3*z1 -y1*z3) + x3*(y1*z2 -y2*z1));
return plane;
}
public static double resolveZ(double x, double y, double[] plane) {
if (plane[2] == 0) {
return Double.MAX_VALUE;
}
return - (plane[0] * x + plane[1] * y + plane[3]) / plane[2];
}
}
31
Output:
32