Ikrimatul Khotimah
Ikrimatul Khotimah
Ikrimatul Khotimah
KELAS
NPM
: IKRIMATUL KHOTIMAH
: S1/TI/6B/PAGI
: 43A87006120121
Hasi Program :
Coding
Triangle.java
class Triangle
{
Point2D a, b, c;
Triangle (Point2D a, Point2D b, Point2D c)
{ this.a = a; this.b = b; this.c = c;
}
}
TriaPoly. Java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class PolyTria extends Frame{
public static void main(String[] args) {new PolyTria();}
PolyTria() //Memanggil Kelas PolyTria
{ super("Define polygon vertices by clicking");
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e) {System.exit(0);}});
setSize (500, 300);
add("Center", new CvPolyTria());
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
show();
}
}
class CvPolyTria extends CvDefPoly // see Section 1.5
{ public void paint(Graphics g)
{ int n = v.size();
if (n > 3 && ready)
{ Point2D[] p = new Point2D[n];
for (int i=0; i<n; i++) p[i] = (Point2D)v.elementAt(i);
// If not counter-clockwise, reverse the order:
if (!ccw(p))
for (int i=0; i<n; i++)
p[i] = (Point2D)v.elementAt(n - i - 1);
int ntr = n - 2;
Triangle[] tr = new Triangle[ntr];
Tools2D.triangulate(p, tr);
initgr();
for (int j=0; j<ntr; j++)
{ g.setColor(new Color(rand(),rand(), rand()));
int[] x = new int[3], y = new int[3];
x[0] = iX(tr[j].a.x); y[0] = iY(tr[j].a.y);
x[1] = iX(tr[j].b.x); y[1] = iY(tr[j].b.y);
x[2] = iX(tr[j].c.x); y[2] = iY(tr[j].c.y);
g.fillPolygon(x, y, 3);
}
}
g.setColor(Color.black);
super.paint(g);
}
int rand() {return (int) (Math.random() * 256); }
static boolean ccw(Point2D[] p)
{ int n = p.length, k = 0;
for (int i=1; i<n; i++)
if (p[i].x <= p[k].x && (p[i].x < p[k].x || p[i].y < p [k].y))
k = i;
// p[k] is a convex vertex.
int prev = k -1, next = k + 1;
Tools2D.java
class Tools2D
{ static float area2(Point2D a, Point2D b, Point2D c)
{return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x-c.x);
}
static boolean insideTriangle (Point2D a, Point2D b,Point2D c,Point2D p )//AbC
{return
Tools2D.area2 (a, b, p) >= 0 &&
Tools2D.area2 (b, c, p) >= 0 &&
Tools2D.area2 (c, a, p) >= 0;
}
static void triangulate (Point2D [] p, Triangle []tr)
{ //
//
//
int n = p.length, j = n - 1, iA=0, iB, iC;
int [] next = new int [n];
for (int i=0; i<n; i ++)
{ next [j] = i;
j = i;
}
for (int k=0; k<n-2; k++)
{//Find a suitable triangle, consisting of two edges
// and an internal diagonal:
Point2D a, b, c;
boolean triaFound = false;
int count = 0;
while (!triaFound && ++count < n )
{ iB = next [iA] ; iC = next [iB];
a = p[iA]; b = p[iB]; c = p[iC];
if (Tools2D.area2(a,b,c)>=0)
{//Edges AB And BC; diagonal AC.
//Test to see if no other polygon vertex
//lies within triangle ABC:
j = next [iC];
while (j!=iA && !insideTriangle(a, b, c, p[j]))
j = next[j];
if (j == iA)
{ // Triangle ABC constains no other vertex:
tr[k]= new Triangle(a, b, c);
next[iA] = iC;
triaFound = true;
}
}
iA = next[iA];
}
if (count == n)
{ System.out.println("Not a simple polygon" + "or vertex sequence not counterclockwise." );
System.exit(1);
}
}
}
static float distance2(Point2D p, Point2D q)
{ float dx = p.x - q.x, dy = p.y - q.y;
return dx * dx + dy * dy;
}
}
CvDefPoly.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
CvDefPoly(){
addMouseListener(new MouseAdapter() {
Point2D.java
class Point2D
{
float x,y;
Point2D (float x,float y){this.x = x; this.y = y;}