Jenis-jenis kurva antara lain :
• Kurva linier: yaitu kurva yang dibentuk dari suatu garis antara 2 buah titik yang saling berhubungan.
• Kurva kubik: yaitu kurva yang memiliki persamaan ax3+bx2+cx+d di mana a, b, c, d adalah konstanta
• Kurva Bézier: yaitu kurva yang proses pembentukannya dari kurva linier, kurva kubik, kuadrat, dan kurva orde ke-4.
Permukaan (surface) merupakan struktur matematis yang terbentuk atas himpunan kurva.
Contoh kurva Bézier dengan menambahkan fitur wireframe dan kendali navigasi untuk merotasi, resize, dan translasi.
-------code-------
import javax.vecmath.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.*;
// Kelas pembentuk bezier surface
public class BezierSurface extends Shape3D {
public BezierSurface(Point3d[][] ctrlPts) {
int m = 17;
int n = 17;
Point3d[] pts = new Point3d[m*n];
int idx = 0;
Point3d[] p = new Point3d[4];
double du = 1.0/(m-1);
double dv = 1.0/(n-1);
double u = 0;
double v = 0;
// Membentuk grid surface
for (int i = 0; i < m; i++) {
for (int k = 0; k < 4; k++) {
p[k] = deCasteljau(u, ctrlPts[k]);
}
v = 0;
for (int j = 0; j < n; j++) {
pts[idx++] = deCasteljau(v, p);
v += dv;
}
u += du;
}
int[] coords = new int[2*n*(m-1)];
idx = 0;
for (int i = 1; i < m; i++) {
for (int j = 0; j < n; j++) {
coords[idx++] = i*n + j;
coords[idx++] = (i-1)*n + j;
}
}
int[] stripCounts = new int[m-1];
for (int i = 0; i < m-1; i++) {
stripCounts[i] = 2*n;
}
GeometryInfo gi =
new GeometryInfo(GeometryInfo.TRIANGLE_STRIP_ARRAY);
gi.setCoordinates(pts);
gi.setCoordinateIndices(coords);
gi.setStripCounts(stripCounts);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(gi);
this.setGeometry(gi.getGeometryArray());
}
// Algoritma de Casteljau's
Point3d deCasteljau(double t, Point3d[] p) {
Point3d[] pt = {new Point3d(p[0]),
new Point3d(p[1]), new Point3d(p[2]), new Point3d(p[3])};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3-i; j++) {
pt[j].interpolate(pt[j+1], t);
}
}
return pt[0];
}
}
------akhir code-------import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.*;
// Kelas pembentuk bezier surface
public class BezierSurface extends Shape3D {
public BezierSurface(Point3d[][] ctrlPts) {
int m = 17;
int n = 17;
Point3d[] pts = new Point3d[m*n];
int idx = 0;
Point3d[] p = new Point3d[4];
double du = 1.0/(m-1);
double dv = 1.0/(n-1);
double u = 0;
double v = 0;
// Membentuk grid surface
for (int i = 0; i < m; i++) {
for (int k = 0; k < 4; k++) {
p[k] = deCasteljau(u, ctrlPts[k]);
}
v = 0;
for (int j = 0; j < n; j++) {
pts[idx++] = deCasteljau(v, p);
v += dv;
}
u += du;
}
int[] coords = new int[2*n*(m-1)];
idx = 0;
for (int i = 1; i < m; i++) {
for (int j = 0; j < n; j++) {
coords[idx++] = i*n + j;
coords[idx++] = (i-1)*n + j;
}
}
int[] stripCounts = new int[m-1];
for (int i = 0; i < m-1; i++) {
stripCounts[i] = 2*n;
}
GeometryInfo gi =
new GeometryInfo(GeometryInfo.TRIANGLE_STRIP_ARRAY);
gi.setCoordinates(pts);
gi.setCoordinateIndices(coords);
gi.setStripCounts(stripCounts);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(gi);
this.setGeometry(gi.getGeometryArray());
}
// Algoritma de Casteljau's
Point3d deCasteljau(double t, Point3d[] p) {
Point3d[] pt = {new Point3d(p[0]),
new Point3d(p[1]), new Point3d(p[2]), new Point3d(p[3])};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3-i; j++) {
pt[j].interpolate(pt[j+1], t);
}
}
return pt[0];
}
}
simpan dengan nama BezierSurface.java
setelah membuat script bentuk kurva ya...nah sekarang kita buat script untuk menjalankan kurva bezier tersebut...berikut script-nya:
:::::::::code::::::::
import javax.vecmath.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.vp.*;
import javax.swing.JFrame;
import java.applet.*;
import com.sun.j3d.utils.applet.MainFrame;
public class DemoBezierSurface extends Applet {
public void init() {
// Menciptakan kanvas
Canvas3D canvas = new Canvas3D(
SimpleUniverse.getPreferredConfiguration());
setLayout(new BorderLayout());
add(canvas, BorderLayout.CENTER);
BranchGroup bg = createSceneGraph();
bg.compile();
SimpleUniverse univ = new SimpleUniverse(canvas);
univ.getViewingPlatform().setNominalViewingTransform();
univ.addBranchGraph(bg);
// Navigasi melalui mouse
OrbitBehavior ob = new OrbitBehavior(canvas);
ob.setSchedulingBounds(new BoundingSphere());
univ.getViewingPlatform().setViewPlatformBehavior(ob);
}
private BranchGroup createSceneGraph() {
BranchGroup root = new BranchGroup();
TransformGroup spin = new TransformGroup();
spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
root.addChild(spin);
// Menciptakan surface
Point3d[][] ctrlPts = new Point3d[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
ctrlPts[i][j] =
new Point3d(2-i, 3*(Math.random()-0.5), j-2);
}
}
Shape3D shape = new BezierSurface(ctrlPts);
Appearance ap = new Appearance();
ap.setMaterial(new Material());
shape.setAppearance(ap);
Transform3D tr = new Transform3D();
tr.setScale(0.25);
TransformGroup tg = new TransformGroup(tr);
spin.addChild(tg);
tg.addChild(shape);
// Mengatur mode wireframe
PolygonAttributes pa = new PolygonAttributes();
pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
ap.setPolygonAttributes(pa);
// interpolator rotasi
Alpha alpha = new Alpha(-1, 10000);
RotationInterpolator rotator =
new RotationInterpolator(alpha, spin);
BoundingSphere bounds = new BoundingSphere();
rotator.setSchedulingBounds(bounds);
spin.addChild(rotator);
// Background dan pencahayaan
Background background = new Background(1f, 1f, 1f);
background.setApplicationBounds(bounds);
root.addChild(background);
AmbientLight light = new AmbientLight(true,
new Color3f(Color.red));
light.setInfluencingBounds(bounds);
root.addChild(light);
PointLight ptlight = new PointLight
(new Color3f(Color.lightGray),
new Point3f(1f,1f,1f), new Point3f(1f,0f,0f));
ptlight.setInfluencingBounds(bounds);
root.addChild(ptlight);
return root;
}
}
:::::::::akhir code:::::::import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.vp.*;
import javax.swing.JFrame;
import java.applet.*;
import com.sun.j3d.utils.applet.MainFrame;
public class DemoBezierSurface extends Applet {
public void init() {
// Menciptakan kanvas
Canvas3D canvas = new Canvas3D(
SimpleUniverse.getPreferredConfiguration());
setLayout(new BorderLayout());
add(canvas, BorderLayout.CENTER);
BranchGroup bg = createSceneGraph();
bg.compile();
SimpleUniverse univ = new SimpleUniverse(canvas);
univ.getViewingPlatform().setNominalViewingTransform();
univ.addBranchGraph(bg);
// Navigasi melalui mouse
OrbitBehavior ob = new OrbitBehavior(canvas);
ob.setSchedulingBounds(new BoundingSphere());
univ.getViewingPlatform().setViewPlatformBehavior(ob);
}
private BranchGroup createSceneGraph() {
BranchGroup root = new BranchGroup();
TransformGroup spin = new TransformGroup();
spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
root.addChild(spin);
// Menciptakan surface
Point3d[][] ctrlPts = new Point3d[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
ctrlPts[i][j] =
new Point3d(2-i, 3*(Math.random()-0.5), j-2);
}
}
Shape3D shape = new BezierSurface(ctrlPts);
Appearance ap = new Appearance();
ap.setMaterial(new Material());
shape.setAppearance(ap);
Transform3D tr = new Transform3D();
tr.setScale(0.25);
TransformGroup tg = new TransformGroup(tr);
spin.addChild(tg);
tg.addChild(shape);
// Mengatur mode wireframe
PolygonAttributes pa = new PolygonAttributes();
pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
ap.setPolygonAttributes(pa);
// interpolator rotasi
Alpha alpha = new Alpha(-1, 10000);
RotationInterpolator rotator =
new RotationInterpolator(alpha, spin);
BoundingSphere bounds = new BoundingSphere();
rotator.setSchedulingBounds(bounds);
spin.addChild(rotator);
// Background dan pencahayaan
Background background = new Background(1f, 1f, 1f);
background.setApplicationBounds(bounds);
root.addChild(background);
AmbientLight light = new AmbientLight(true,
new Color3f(Color.red));
light.setInfluencingBounds(bounds);
root.addChild(light);
PointLight ptlight = new PointLight
(new Color3f(Color.lightGray),
new Point3f(1f,1f,1f), new Point3f(1f,0f,0f));
ptlight.setInfluencingBounds(bounds);
root.addChild(ptlight);
return root;
}
}
ok...selesai deh dalam membuat script untuk menjalankan kurva bezier.
Dari 2 program diatas, maka akan didapat hasil sebagai berikut:
jalankan lewat html dengan cara buat script ini di notepad n sejenisya..tergantung apa yang anda suka..
<html>
<body>
<applet code = "DemoBezierSurface.class" width= "500" height="500">
</applet>
</body>
</html>
simpan dengan nama bebas terserah kreasi anda...dari ke-3 program diatas disimpan dalam 1 folder..<body>
<applet code = "DemoBezierSurface.class" width= "500" height="500">
</applet>
</body>
</html>
cara compile or menjalankannya sebagai berikut:
lewat commandprompt
javac BezierSurface.java
javac DemoBezierSurface.java
panggil dengan nama AppletViewer DemoBezierSurface.html
Nah ini hasil dari compile programnya:
Tidak ada komentar:
Posting Komentar