curve in this way...help

hi,

i have to join two line with smooth curve

eg.

...................................................o2

.......................o1

............................p1

....................................................p2

// This dash-dash point are nothing

Suppose given o1(x1,y1) & o2(x2,y2) are point and line joining them is O same asp1(x3,y3) & p2(x4,y4) are point and line joining them is p

Now i want to draw smooth curve (Circle, ellipse) joining points o1 &p1.

How to do this. Actually i have to draw contour ,plz help me

Message was edited by:

vivind

[650 byte] By [vivinda] at [2007-11-27 6:04:00]
# 1

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

public class SmoothJoin extends JPanel {

Point2D.Double[] points;

CubicCurve2D.Double curve;

public SmoothJoin() {

int[] coords = {

150, 150, 230, 75, 200, 275, 350, 350, 0, 0, 0, 0

};

points = new Point2D.Double[6];

for(int j = 0, k = 0; j < points.length; j++) {

points[j] = new Point2D.Double(coords[k++], coords[k++]);

}

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

if(curve == null)

setCurve();

g2.setPaint(Color.orange);

g2.draw(curve);

g2.setPaint(Color.blue);

g2.draw(new Line2D.Double(points[0], points[1]));

g2.draw(new Line2D.Double(points[2], points[3]));

for(int j = 0; j < points.length; j++) {

Color color = j < 4 ? Color.red : Color.green.darker();

g2.setPaint(color);

mark(points[j], g2);

}

}

private void setCurve() {

if(curve == null)

curve = new CubicCurve2D.Double();

double x1 = points[0].x;

double y1 = points[0].y;

Point2D.Double ip = getIntersection();

double ctrlx1 = ip.x;

double ctrly1 = ip.y;

double ctrlx2 = ip.x;

double ctrly2 = ip.y;

double x2 = points[2].x;

double y2 = points[2].y;

curve.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);

points[4].x = ctrlx1;

points[4].y = ctrly1;

points[5].x = ctrlx2;

points[5].y = ctrly2;

}

private Point2D.Double getIntersection() {

// Extend the lines to get an intersection.

// You may have to confirm an intersection.

int w = getWidth();

int h = getHeight();

double diag = Math.sqrt(w*w + h*h);

double theta = getAngle(points[0], points[1]);

Point2D.Double p = new Point2D.Double();

p.x = points[1].x + diag*Math.cos(theta);

p.y = points[1].y + diag*Math.sin(theta);

Line2D.Double line1 = new Line2D.Double(p, points[1]);

theta = getAngle(points[2], points[3]);

p.x = points[2].x + diag*Math.cos(theta);

p.y = points[2].y + diag*Math.sin(theta);

Line2D.Double line2 = new Line2D.Double(p, points[3]);

return getIntersection(line1, line2);

}

private double getAngle(Point2D.Double p1, Point2D.Double p2) {

double dy = p1.y - p2.y;

double dx = p1.x - p2.x;

return Math.atan2(dy, dx);

}

private Point2D.Double getIntersection(Line2D.Double line1, Line2D.Double line2) {

double x1 = line1.getX1();

double y1 = line1.getY1();

double x2 = line1.getX2();

double y2 = line1.getY2();

double x3 = line2.getX1();

double y3 = line2.getY1();

double x4 = line2.getX2();

double y4 = line2.getY2();

double aDividend = (x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3);

double aDivisor = (y4 - y3)*(x2 - x1) - (x4 - x3)*(y2 - y1);

double ua = aDividend / aDivisor;

double bDividend = (x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3);

double bDivisor = (y4 - y3)*(x2 - x1) - (x4 - x3)*(y2 - y1);

double ub = bDividend / bDivisor;

Point2D.Double p = new Point2D.Double();

p.x = x1 + ua * (x2 - x1);

p.y = y1 + ua * (y2 - y1);

return p;

}

private void mark(Point2D.Double p, Graphics2D g2) {

g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));

}

public static void main(String[] args) {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new SmoothJoin());

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-12 16:47:37 > top of Java-index,Security,Cryptography...