summaryrefslogtreecommitdiffstats
path: root/šola/p1/dn/6/Premica.java
blob: 2d2ec4e40d8a8a0d4808a2738088f0e16b5ed8eb (plain) (blame)
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
46
47
48
49
50
import java.util.*;
public class Premica {
	private double k;
	private double n;
	public Premica(double k, double n) {
		this.k = k;
		this.n = n;
	}
	public double vrniK() {
		return this.k;
	}
	public double vrniN() {
		return this.n;
	}
	public String toString() {
		return String.format(Locale.ROOT, "y = %.2f x + %.2f", this.k, this.n);
	}
	public Tocka tockaPriX(double x) {
		return new Tocka(x, this.k*x+this.n);
	}
	public static Premica skoziTocko(double k, Tocka t) {
		return new Premica(k, t.vrniY()-k*t.vrniX());
	}

	public Premica vzporednica(Tocka t) {
		return skoziTocko(this.k, t);
	}

	public Premica pravokotnica(Tocka t) {
		return skoziTocko(-1/this.k, t);
	}

	public Tocka presecisce(Premica p, double epsilon) {
		if (Math.abs(this.k - p.vrniK()) < epsilon)
			return null;
		return tockaPriX((this.n-p.vrniN())/(p.vrniK()-this.k));
	}
	public Tocka projekcija(Tocka t) {
		return presecisce(pravokotnica(t), 0);
	}
	public double razdalja(Tocka t) {
		return t.razdalja(projekcija(t));
	}
	public double razdaljaOdIzhodisca() {
		return Tocka.izhodisce().razdalja(projekcija(Tocka.izhodisce()));
	}
	public double razdalja(double n) {
		return new Premica(this.k, n).razdalja(tockaPriX(0));
	}
}