summaryrefslogtreecommitdiff
path: root/src/function/Display.java
blob: 3241c5cd0106091e1f16505674733852b5173997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package function;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;


public class Display {
	
	private int x, y, width, height, xMin, xMax, yMin, yMax;
	private ArrayList<Function> functions;
	
	public Display(int x, int y, int width, int height, int xMin, int xMax, int yMin, int yMax, ArrayList<Function> functions) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.xMin = xMin;
		this.xMax = xMax;
		this.yMin = yMin;
		this.yMax = yMax;
		this.functions = functions;
	}
	
	public Display(int x, int y, int width, int height, int xMin, int xMax, int yMin, int yMax) {
		this(x, y, width, height, xMin, xMax, yMin, yMax, new ArrayList<Function>());
	}
	
	public void draw(Graphics g) {
		for(int i = 0, size = functions.size(); i<size; i++) {
			for(int x = 0; x<width; x++) {
				int y1 = (int) Math.round(functions.get(i).getY(x*((double)(xMax-xMin)/width))*((double)height/(yMax-yMin))) + (height/(yMax-yMin));
				int y2 = (int) Math.round(functions.get(i).getY((x+1)*((double)(xMax-xMin)/width))*((double)height/(yMax-yMin))) + (height/(yMax-yMin));
				if(y1 >= 0 && y1 <= height && y2 >= 0 && y2 <= height)
					g.drawLine(this.x+x, this.y+y1, this.x+x+1, this.y+y2);
			}
		}
		g.setColor(Color.BLACK);
		g.drawRect(x, y, width, height);
	}
	
	public void addFunction(Function f) {
		functions.add(f);
	}

}