下面列出了java.awt.geom.CubicCurve2D#Double ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Shape getRegiaoDocumento() {
if (Regiao == null) {
final int v1 = getHeight() / 3;
final int h1 = getWidth() / 2;
final int repo = v1 / 3;
final int L = getLeft();
final int T = getTop();
final int TH = T + getHeight() - repo;
final int LW = L + getWidth();
CubicCurve2D c = new CubicCurve2D.Double();
c.setCurve(L, TH, L + h1, TH + v1, LW - h1, TH - v1, LW, TH);
GeneralPath pa = new GeneralPath();
pa.moveTo(LW, TH);
pa.lineTo(LW, T);
pa.lineTo(L, T);
pa.lineTo(L, TH);
pa.append(c, true);
Regiao = pa;
final int ptToMove = 3;
this.reposicionePonto[ptToMove] = new Point(0, -repo);
ptsToMove[ptToMove] = 1;
}
return Regiao;
}
/**
* Given 0<=t<=1 and an existing curve, divide it into two chunks and store the
* two chunks into "first" and "second"
*/
public static void divide(double t, CubicCurve2D.Double curve, CubicCurve2D.Double first, CubicCurve2D.Double second) {
// This algorithm uses de Casteljau's algorithm for chopping one bezier
// curve into two bezier curves
first.x1 = curve.x1;
second.x2 = curve.x2;
first.ctrlx1 = (1 - t) * curve.x1 + t * curve.ctrlx1;
double x = (1 - t) * curve.ctrlx1 + t * curve.ctrlx2;
second.ctrlx2 = (1 - t) * curve.ctrlx2 + t * curve.x2;
first.ctrlx2 = (1 - t) * first.ctrlx1 + t * x;
second.ctrlx1 = (1 - t) * x + t * second.ctrlx2;
second.x1 = first.x2 = (1 - t) * first.ctrlx2 + t * second.ctrlx1;
// now that we've computed the x coordinates, we now compute the y
// coordinates
first.y1 = curve.y1;
second.y2 = curve.y2;
first.ctrly1 = (1 - t) * curve.y1 + t * curve.ctrly1;
double y = (1 - t) * curve.ctrly1 + t * curve.ctrly2;
second.ctrly2 = (1 - t) * curve.ctrly2 + t * curve.y2;
first.ctrly2 = (1 - t) * first.ctrly1 + t * y;
second.ctrly1 = (1 - t) * y + t * second.ctrly2;
second.y1 = first.y2 = (1 - t) * first.ctrly2 + t * second.ctrly1;
}
public static void main(String[] args) throws Exception {
CubicCurve2D c = new CubicCurve2D.Double(0, 0, 4, -4, -2, -4, 2, 0);
Rectangle2D r = new Rectangle2D.Double(0.75, -2.5, 0.5, 2);
if (c.contains(r)) {
throw new Exception("The rectangle should not be contained in the curve");
}
}
public static void main(String[] args) throws Exception {
CubicCurve2D c = new CubicCurve2D.Double(50.0, 300.0,
150.0, 166.6666717529297,
238.0, 456.66668701171875,
350.0, 300.0);
Rectangle2D r = new Rectangle2D.Double(260, 300, 10, 10);
if (!c.intersects(r)) {
throw new Exception("The rectangle is contained. " +
"intersects(Rectangle2D) should return true");
}
}
public static void main(String[] args) throws Exception {
CubicCurve2D c = new CubicCurve2D.Double(0, 0, 4, -4, -2, -4, 2, 0);
Rectangle2D r = new Rectangle2D.Double(0.75, -2.5, 0.5, 2);
if (c.contains(r)) {
throw new Exception("The rectangle should not be contained in the curve");
}
}
private static Shape makeCurve(int x1, int x2, int y1, int y2, boolean forward) {
int width = x2 - x1;
if (forward) {
return new CubicCurve2D.Double(x1, y1,
x1 + width * CTRL_PROXIMITY_X, y1,
x1 + width * (1.0 - CTRL_PROXIMITY_X), y2,
x1 + width, y2);
}
else {
return new CubicCurve2D.Double(x1 + width, y2,
x1 + width * (1.0 - CTRL_PROXIMITY_X), y2,
x1 + width * CTRL_PROXIMITY_X, y1,
x1, y1);
}
}
public static void main(String[] args) throws Exception {
CubicCurve2D c = new CubicCurve2D.Double(0, 0, 4, -4, -2, -4, 2, 0);
Rectangle2D r = new Rectangle2D.Double(0.75, -2.5, 0.5, 2);
if (c.contains(r)) {
throw new Exception("The rectangle should not be contained in the curve");
}
}
public static void main(String[] args) throws Exception {
CubicCurve2D c = new CubicCurve2D.Double(50.0, 300.0,
150.0, 166.6666717529297,
238.0, 456.66668701171875,
350.0, 300.0);
Rectangle2D r = new Rectangle2D.Double(260, 300, 10, 10);
if (!c.intersects(r)) {
throw new Exception("The rectangle is contained. " +
"intersects(Rectangle2D) should return true");
}
}
/**
* Report the Bezier curve which best approximates the circle arc.
*
* @return the Bezier curve
*/
public CubicCurve2D.Double getCurve ()
{
if (curve == null) {
computeCurve();
}
return curve;
}
private static Shape makeCurve(int width, int y1, int y2, boolean forward) {
if (forward) {
return new CubicCurve2D.Double(0, y1,
width * CTRL_PROXIMITY_X, y1,
width * (1.0 - CTRL_PROXIMITY_X), y2,
width, y2);
}
else {
return new CubicCurve2D.Double(width, y2,
width * (1.0 - CTRL_PROXIMITY_X), y2,
width * CTRL_PROXIMITY_X, y1,
0, y1);
}
}
public static void main(String[] args) throws Exception {
CubicCurve2D c = new CubicCurve2D.Double(50.0, 300.0,
150.0, 166.6666717529297,
238.0, 456.66668701171875,
350.0, 300.0);
Rectangle2D r = new Rectangle2D.Double(260, 300, 10, 10);
if (!c.intersects(r)) {
throw new Exception("The rectangle is contained. " +
"intersects(Rectangle2D) should return true");
}
}
public static void main(String[] args) throws Exception {
CubicCurve2D c = new CubicCurve2D.Double(50.0, 300.0,
150.0, 166.6666717529297,
238.0, 456.66668701171875,
350.0, 300.0);
Rectangle2D r = new Rectangle2D.Double(260, 300, 10, 10);
if (!c.intersects(r)) {
throw new Exception("The rectangle is contained. " +
"intersects(Rectangle2D) should return true");
}
}
void bendUp(double x, double y1, double y2, double gap) {
for (int i = 0; i < list.size(); i++) {
CubicCurve2D.Double c = list.get(i);
if (intersects(c.x1, c.y1, c.x2, c.y2, x, y1, y2)) {
list.set(i, makeline(c.x1, c.y1, x, y1 - gap));
list.add(i + 1, makeline(x, y1 - gap, c.x2, c.y2));
return;
}
}
}
private void paintSpline(Graphics2D g2) {
CubicCurve2D spline = new CubicCurve2D.Double(xPositionToPixel(0.0), yPositionToPixel(0.0),
xPositionToPixel(control1.getX()),
yPositionToPixel(control1.getY()),
xPositionToPixel(control2.getX()),
yPositionToPixel(control2.getY()),
xPositionToPixel(1.0), yPositionToPixel(1.0));
g2.setColor(new Color(0.0f, 0.3f, 0.0f, 1.0f));
g2.draw(spline);
}
private CubicCurve2D getPartialCurve(double tStart, double tEnd) {
CubicCurve2D fullCurve = new CubicCurve2D.Double();
fullCurve.setCurve(connectionInfo.getFirstCenter(), cp1.getPosition(), cp2.getPosition(), connectionInfo.getSecondCenter());
CurveSplitResult firstSplit = Geometry.splitCubicCurve(fullCurve, tStart);
double t = (tEnd - tStart) / (1 - tStart);
return Geometry.splitCubicCurve(firstSplit.curve2, t).curve1;
}
@Override
public void DoPaint(Graphics2D g) {
int l = left;
int t = top;
if (!outroPintor) {
if (getTipo() == tipoDrawer.tpMedida) {
l = dono.getL() + left;
t = dono.getT() + top;
} else {
l = dono.getL();
t = dono.getT();
}
}
if (!recivePaint) {
if (isGradiente()) {
g.setPaint(PaintGradiente(g, l, t));
} else {
g.setColor(getCor());
}
}
Shape dr = null;
boolean ok = false;
int[] pts;
switch (tipo) {
case tpElipse:
pts = ArrayDePontos(getElipse());
if (pts.length == 4) {
dr = new Ellipse2D.Double(pts[0], pts[1], pts[2], pts[3]);
}
break;
case tpRetangulo:
pts = ArrayDePontos(getRetangulo());
if (pts.length == 4) {
dr = new Rectangle2D.Double(pts[0], pts[1], pts[2], pts[3]);
}
if (pts.length == 6) {
dr = new RoundRectangle2D.Double(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
}
break;
case tpCurva:
pts = ArrayDePontos(getCurva());
if (pts.length == 8) {
dr = new CubicCurve2D.Double(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5], pts[6], pts[7]);
}
if (pts.length == 6) {
dr = new QuadCurve2D.Double(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
}
break;
case tpArco:
// public final static int OPEN = 0;
// public final static int CHORD = 1;
// public final static int PIE = 2;
pts = ArrayDePontos(getArco());
if (pts.length == 7) {
dr = new Arc2D.Double(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5], pts[6]);
}
break;
case tpImagem:
DrawImagem(g);
ok = true;
break;
case tpMedida:
//# Foi retirado do dezenho porque está feio (DrawerEditor).)
if (vertical == VERTICAL) {
medidaH(g, l, t);
} else {
medidaV(g, l, t);
}
ok = true;
break;
case tpPath:
DrawComplexPath(g, l, t);
ok = true;
break;
default:
g.drawLine(l, t, getWidth(), getHeight());
ok = true;
break;
}
if (dr == null || ok) {
if (dr == null && !ok) {
g.drawString("?", l + 5, t + 5);
}
return;
}
if (isFill()) {
g.fill(dr);
} else {
g.draw(dr);
}
}
/**
* Report the left-to-right Bézier curve which best approximates the slur.
* <p>
* It is built by combining the left half (point & control point) of left circle curve and
* the right half (control point & point) of right circle curve.
* Vectors from point to related control point are applied a ratio extension so that curve
* middle point (M) fits on slur middle point (M').
* We apply the same ratio on both vectors, which may not be the best choice but that's enough
* for a first version.
* On a bezier curve, naming P the middle point of segment (P1,P2) and C the middle point of
* segment (CP1,CP2), we always have vector PC = 4/3 of vector PM.
* So, (PC' - PC) = 4/3 (PM' - PM) or (ratio - 1) * PC = 4/3 * deltaM, which gives ratio value.
*
* @return the bezier curve
*/
public CubicCurve2D getCurve ()
{
if (curve == null) {
Model leftModel = getSideModel(true);
Model rightModel = getSideModel(false);
if ((leftModel == null) || (rightModel == null)) {
///logger.warn("No side circle");
return null;
}
// Assume we have circle models on both ends
if (!(leftModel instanceof CircleModel) || !(rightModel instanceof CircleModel)) {
return null;
}
CubicCurve2D left = (CubicCurve2D) leftModel.getCurve();
CubicCurve2D right = (CubicCurve2D) rightModel.getCurve();
if (left == right) {
curve = left;
} else {
double x1 = left.getX1();
double y1 = left.getY1();
double cx1 = left.getCtrlX1();
double cy1 = left.getCtrlY1();
double cx2 = right.getCtrlX2();
double cy2 = right.getCtrlY2();
double x2 = right.getX2();
double y2 = right.getY2();
// Compute affinity ratio out of mid point translation
Point midPt = points.get(points.size() / 2); // Approximately
double mx = (x1 + x2 + (3 * (cx1 + cx2))) / 8;
double my = (y1 + y2 + (3 * (cy1 + cy2))) / 8;
double deltaM = Math.hypot(midPt.x - mx, midPt.y - my);
double pc = Math.hypot((cx1 + cx2) - (x1 + x2), (cy1 + cy2) - (y1 + y2)) / 2;
double ratio = 1 + ((4 * deltaM) / (3 * pc));
// Apply ratio on vectors to control points
curve = new CubicCurve2D.Double(
x1,
y1,
x1 + (ratio * (cx1 - x1)), // cx1'
y1 + (ratio * (cy1 - y1)), // cy1'
x2 + (ratio * (cx2 - x2)), // cx2'
y2 + (ratio * (cy2 - y2)), // cy2'
x2,
y2);
}
}
return curve;
}
/**
* Create the natural spline that interpolates the provided knots
*
* @param xx the abscissae of the provided points
* @param yy the ordinates of the provided points
* @return the resulting spline curve
*/
public static NaturalSpline interpolate (double[] xx,
double[] yy)
{
// Check parameters
Objects.requireNonNull(xx, "NaturalSpline cannot interpolate null arrays");
Objects.requireNonNull(yy, "NaturalSpline cannot interpolate null arrays");
if (xx.length != yy.length) {
throw new IllegalArgumentException(
"NaturalSpline interpolation needs consistent coordinates");
}
// Number of segments
final int n = xx.length - 1;
if (n < 1) {
throw new IllegalArgumentException(
"NaturalSpline interpolation needs at least 2 points");
}
if (n == 1) {
// Use a Line
return new NaturalSpline(new Line2D.Double(xx[0], yy[0], xx[1], yy[1]));
} else if (n == 2) {
// Use a Quadratic (TODO: check this formula...)
// double t = (xx[1] - xx[0]) / (xx[2] - xx[0]);
// double u = 1 - t;
// double cpx = (xx[1] - (u * u * xx[0]) - (t * t * xx[2])) / 2 * t * u;
// double cpy = (yy[1] - (u * u * yy[0]) - (t * t * yy[2])) / 2 * t * u;
return new NaturalSpline(
new QuadCurve2D.Double(
xx[0],
yy[0],
(2 * xx[1]) - ((xx[0] + xx[2]) / 2),
(2 * yy[1]) - ((yy[0] + yy[2]) / 2),
xx[2],
yy[2]));
} else {
// Use a sequence of cubics
double[] dx = getCubicDerivatives(xx);
double[] dy = getCubicDerivatives(yy);
Shape[] curves = new Shape[n];
for (int i = 0; i < n; i++) {
// Build each segment curve
curves[i] = new CubicCurve2D.Double(
xx[i],
yy[i],
xx[i] + (dx[i] / 3),
yy[i] + (dy[i] / 3),
xx[i + 1] - (dx[i + 1] / 3),
yy[i + 1] - (dy[i + 1] / 3),
xx[i + 1],
yy[i + 1]);
}
return new NaturalSpline(curves);
}
}
@Override
public Shape getRegiao() {
if (Regiao == null) {
final int v1 = getHeight() / 3;
final int h1 = getWidth() / 2;
final int repo = v1 / 3;
final int L = getLeft();
int recuo = h1/8;
final int T = getTop() + recuo;
final int TH = T + getHeight() - repo -recuo;
final int LW = L + getWidth() -recuo;
CubicCurve2D c = new CubicCurve2D.Double();
c.setCurve(L, TH, L + h1, TH + v1, LW - h1, TH - v1, LW, TH);
GeneralPath pa = new GeneralPath();
pa.setWindingRule(GeneralPath.WIND_EVEN_ODD);
pa.moveTo(LW, TH);
pa.lineTo(LW, T);
pa.lineTo(L, T);
pa.lineTo(L, TH);
pa.append(c, false);
int tam = recuo /2;
pa.moveTo(L + tam, T);
pa.lineTo(L + tam, T - tam);
pa.lineTo(LW + tam, T - tam);
pa.lineTo(LW + tam, TH - tam);
pa.lineTo(LW, TH -tam);
pa.lineTo(LW, T);
pa.lineTo(L + tam, T);
tam = recuo;
pa.moveTo(L + tam, T - (tam/2));
pa.lineTo(L + tam, T - tam);
pa.lineTo(LW + tam, T - tam);
pa.lineTo(LW + tam, TH - tam);
pa.lineTo(LW + (tam/2), TH -tam);
pa.lineTo(LW + (tam/2), T -(tam/2));
pa.lineTo(L + tam, T - (tam/2));
pa.closePath();
Regiao = pa;
this.reposicionePonto[ptToMove] = new Point(-tam/2, -repo);
ptsToMove[ptToMove] = 1;
}
return Regiao;
}
/**
* Produce a cubic bezier curve representing a straightline segment from (x1,y1)
* to (x2,y2)
*/
private static CubicCurve2D.Double makeline(double x1, double y1, double x2, double y2) {
return new CubicCurve2D.Double(x1, y1, (x2 - x1) * 0.3 + x1, (y2 - y1) * 0.3 + y1, (x2 - x1) * 0.6 + x1, (y2 - y1) * 0.6 + y1, x2, y2);
}