Skip to content

Instantly share code, notes, and snippets.

@redhead
Created January 14, 2011 12:08
Show Gist options
  • Save redhead/779531 to your computer and use it in GitHub Desktop.
Save redhead/779531 to your computer and use it in GitHub Desktop.
public class Primka {
// rovnice primky je dana:
// y = k*x + q
public double k; // k - smernice primky
public double q; // q - posun v ose y
// kostruktor, naplni instacni promenne
// hodnotami danymi pri vytvoreni objektu:
// new Primka(2, -1) je primka y = 2x - 1
public Primka(double k, double q) {
this.k = k;
this.q = q;
}
public static double[] prusecik(Primka a, Primka b) {
/**
resime 2 rovnice primek,
prusecik nalezneme, polozime-li rovnice sobe rovny
k1*x + q1 = k2*x + q2
*/
double k = a.k - b.k; // odecteme smernice obou dostaneme nejake k*x
double q = b.q - a.q; // odecteme posun v y (musi se odecist opacne: b-a)
double x = q / k; // x pruseciku: z k*x vyjadrime x vydelenim k (kx = q => x = q/k)
double y = a.k * x + a.q; // y pruseciku: dosadime do rovnice prvni primky vypoctene x => (y = k1*x + q1)
return new double[] { x, y }; // vratime pole sestavajici z x-ové a y-ové souradnice pruseciku
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// MAIN metoda v Main.java:
public static void main(String[] args) {
Primka a = new Primka(1, 2); // y = x + 2
Primka b = new Primka(-1, 2); // y = -x + 2
double[] prusecik = Primka.prusecik(a, b);
System.out.println("x: " + prusecik[0] + " y: " + prusecik[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment