lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 4th order bezier bows


From: Rune Zedeler
Subject: Re: 4th order bezier bows
Date: Fri, 22 Feb 2002 16:56:46 +0100

Rune Zedeler wrote:

> Attached a small slammed java-program 

Yep.
import java.awt.*;
import java.awt.event.*;

public class Bezier extends Component
    implements MouseListener, MouseMotionListener, WindowListener {
    private double[] coords;
    private int order;
    private int status=0;
    private int width;
    private int height;
    public static void main(String[] args) {
        int o = args.length == 0 ? 3 :
            Integer.parseInt(args[0]);
        new Bezier(o).repaint();
    }
    public Bezier(int o) {
        super();
        Frame fr = new Frame();
        fr.setTitle("Bezier (Order: "+o+")");
        fr.setBackground(Color.white);
        fr.add(this);
        fr.pack();
        fr.setVisible(true);
        fr.addWindowListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);
        order = o;
        coords = new double[2*(order+1)];
        for(int i=0; i<=order; i++) {
            coords[2*i]=.1+.8*i/order;
            coords[2*i+1]=.5;
        }
    }
    public Dimension getPreferredSize() {
        return new Dimension(640,400);
    }
    public void paint(Graphics g) {
        Dimension size = getSize();
        width=(int)size.getWidth();
        height=(int)size.getHeight();
        double[]a=new double[2*(order+1)];
        for(int i=0; i<2*(order+1); i+=2) {
            a[i]=coords[i]*width;a[i+1]=coords[i+1]*height;
        }
        g.setColor(Color.gray);
        for(int i=0; i<2*order; i+=2)
            drawDragLine((int)a[i],(int)a[i+1],(int)a[i+2],(int)a[i+3],g);
        g.setColor(Color.black);
        spline(a,g);
    }
    public static void drawDragLine(int x1, int y1, int x2, int y2, Graphics g) 
{
        g.setColor(Color.gray);
        g.drawLine(x1,y1,x2,y2);
        g.setColor(Color.blue);
        g.drawRect(x1-3,y1-3,6,6);
        g.drawRect(x2-3,y2-3,6,6);
    }
    private void spline(double[] pts, Graphics g) {
        boolean close_enough = true;
        for(int i=0; i<2*order; i++)
            close_enough &= Math.abs(pts[i]-pts[i+2])<2;
        if(close_enough) {
            g.drawLine((int)(pts[0]+.5),(int)(pts[1]+.5),
                       (int)(pts[pts.length-2]+.5),(int)(pts[pts.length-1]+.5));
        }
        else {
            double[] a=new double[2*(order+1)];
            double[] b=new double[2*(order+1)];
            for(int i=0; i<=order; i++) {
                a[i*2]=pts[0]; a[i*2+1]=pts[1];
                b[2*(order-i)]=pts[2*(order-i)];
                b[2*(order-i)+1]=pts[2*(order-i)+1];
                pts = med(pts);
            }
            spline(a,g);
            spline(b,g);
        }
    }
    public static double[] med(double[] ar) {
        double[] med=new double[ar.length-2];
        for(int i=0; i<med.length; i++) {
            med[i]=(ar[i]+ar[i+2])/2;
        }
        return med;
    }

    public void mouseClicked(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
        status=0;
        for(int i=0; i<=order; i++) {
            if(Math.abs(coords[2*i]*width-e.getX())<=3 &&
               Math.abs(coords[2*i+1]*height-e.getY())<=3) {
                status=i+1;
            }
        }
        if (status>0) mouseDragged(e);
    }
    public void mouseReleased(MouseEvent e) {
        status=0;
        repaint();
    }
    public void mouseDragged(MouseEvent e) {
        if(status>0) {
            double x = Math.min(1,Math.max(0,(double)e.getX()/width));
            double y = Math.min(1,Math.max(0,(double)e.getY()/height));
            coords[2*status-2]=x;
            coords[2*status-1]=y;
            repaint();
        }
    }
    public void mouseMoved(MouseEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }
    public void windowClosed(WindowEvent e) {
        System.exit(0);
    }
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
    public void windowDeactivated(WindowEvent e) {
    }
    public void windowDeiconified(WindowEvent e) {
    }
    public void windowIconified(WindowEvent e) {
    }
    public void windowOpened(WindowEvent e) {
    }
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]