view src/samer/maths/VectorEditor.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line source
/*
 *	VectorEditor.java	
 *
 *	Copyright (c) 2001, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.maths;

import  samer.core.*;
import  java.awt.*;
import  java.awt.event.*;
import  java.util.*;

public class VectorEditor extends VectorPlotter 
	implements MouseListener, MouseMotionListener
{
	Mat		mat;
	double	lasty;
	int		lasti;

	public VectorEditor(Observable o, Vec s) {
		super(o,s);
		addMouseListener(this);
		addMouseMotionListener(this);
		mat=s.mat();
	}

	private void handle(MouseEvent e,boolean dragging)
	{
		// get inverse mapping of position
		double x = xmap.inverseFromInt(e.getX());
		double y = ymap.inverseFromInt(height-e.getY());
		int    i = (int)x;

		if (i>=0 && i<mat.width()) {
			if (dragging) {
				if (i>lasti) {
					for (int j=lasti+1; j<=i; j++) {
						double t=(j-lasti)/(float)(i-lasti);
						double yy = t*y + (1-t)*lasty;
						mat.set(1,j,yy);
					}
				} else if (i<lasti) {
					for (int j=lasti-1; j>=i; j--) {
						double t=(j-lasti)/(float)(i-lasti);
						double yy = t*y + (1-t)*lasty;
						mat.set(1,j,yy);
					}
				} else {
					mat.set(1,i,y);
				}
			} else {
				mat.set(1,i,y);
			}
			obs.notifyObservers(null);
			lasty=y;
			lasti=i;
		}
	}

	public void mouseMoved(MouseEvent e) {}
	public void mouseDragged(MouseEvent e) { 
		int f = e.getModifiers();
		if ((f & InputEvent.BUTTON1_MASK)!=0) {
			handle(e,true); e.consume(); 
		}
	}
	public void mousePressed(MouseEvent e) { 
		int f = e.getModifiers();
		if ((f & InputEvent.BUTTON1_MASK)!=0) {
			handle(e,false); e.consume(); 
		}
	}
	public void mouseReleased(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
}