2017-12-18 138 views
0

在這裏是與總共4個控制點創建Bezier曲線的代碼:如何添加額外的控制點到貝塞爾曲線?

  • 藍顏色是起始和結束的控制點。
  • 青色是第二和第三個控制點。

我想一個控制點從這些3個控制點的添加到現有的曲線,所以現在我有3個青色的彩色控制點,這樣我可以調整曲線(如拖動曲線),而不是2個控制點。這需要做什麼?

我附上了下面的代碼的輸出作爲附件。

import java.awt.Color; 
import java.awt.Frame; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.geom.GeneralPath; 

public class Piyush extends Frame implements MouseListener, MouseMotionListener { 

// private int[] xs = { 75, 150, 300, 375 }; 
// 
// private int[] ys = { 250, 100, 350, 250 }; 

    private int[] xs = { 75, 200, 300, 375 }; 

    private int[] ys = { 250, 100, 100, 250 }; 

    private int dragIndex = NOT_DRAGGING; 

    private final static int NEIGHBORHOOD = 15; 

    private final static int NOT_DRAGGING = -1; 

    public static void main(String[] args) { 
    new Piyush(); 
    } 

    public Piyush() { 
    setSize(500, 450); 
    addMouseListener(this); 
    addMouseMotionListener(this); 
    addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent e) { 
     System.exit(0); 
     } 
    }); 
    setVisible(true); 
    } 

    public void paint(Graphics g) { 
    for (int i = 0; i < 4; i++) { 
     if (i == 0 || i == 3){ 
     g.setColor(Color.blue); 
     g.fillOval(xs[i] - 6, ys[i] - 6, 12, 12);} 
     else{ 
     g.setColor(Color.cyan); 
     g.fillOval(xs[i] - 6, ys[i] - 6, 12, 12); 
     } 

    } 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.setColor(Color.black); 
    GeneralPath path = new GeneralPath(); 
    path.moveTo(xs[0], ys[0]); 
    path.curveTo(xs[1], ys[1], xs[2], ys[2], xs[3], ys[3]); 
    g2d.draw(path); 
    } 

    public void mousePressed(MouseEvent e) { 
    dragIndex = NOT_DRAGGING; 
    int minDistance = Integer.MAX_VALUE; 
    int indexOfClosestPoint = -1; 
    for (int i = 0; i < 4; i++) { 
     int deltaX = xs[i] - e.getX(); 
     int deltaY = ys[i] - e.getY(); 
     int distance = (int) (Math.sqrt(deltaX * deltaX + deltaY * deltaY)); 
     if (distance < minDistance) { 
     minDistance = distance; 
     indexOfClosestPoint = i; 
     } 
    } 
    if (minDistance > NEIGHBORHOOD) 
     return; 

    dragIndex = indexOfClosestPoint; 
    } 

    public void mouseReleased(MouseEvent e) { 
    if (dragIndex == NOT_DRAGGING) 
     return; 
    xs[dragIndex] = e.getX(); 
    ys[dragIndex] = e.getY(); 
    dragIndex = NOT_DRAGGING; 
    repaint(); 
    } 

    public void mouseDragged(MouseEvent e) { 
    if (dragIndex == NOT_DRAGGING) 
     return; 

    xs[dragIndex] = e.getX(); 
    ys[dragIndex] = e.getY(); 
    repaint(); 
    } 

    public void mouseClicked(MouseEvent e) { 
    } 

    public void mouseEntered(MouseEvent e) { 
    } 

    public void mouseExited(MouseEvent e) { 
    } 

    public void mouseMoved(MouseEvent e) { 
    } 
} 
+0

[https://en.wikipedia.org/wiki/B%C3%A9zier_curve ](Bézier曲線)通常只有四個控制點,除非你想移動到[高階曲線](https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Higher-order_curves)哪種情況下你只需要實現更高階的數學。 – OldCurmudgeon

回答

0
+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。僅僅是一個鏈接的答案可能會被刪除。請參閱https://stackoverflow.com/help/deleted-answers – Spangen

+0

嗨Spangen只是讓你知道原始代碼來自哪裏。 – snoopy11

+0

第一個鏈接似乎被打破。這個問題作者是否在這個論壇上交叉發佈? – halfer