01 Geometry CPP Java

background image

C++:

Java:

//Geometria.cpp
#include <iostream.h>

class Punkt2D {

public:
double x, y;

};

class Linia2D {
public:

Punkt2D p1, p2;
void skaluj(double d){

Punkt2D c;
c.x = (p1.x+p2.x)/2;

c.y = (p1.y+p2.y)/2;
p1.x = c.x+(p1.x-c.x)* d;

p1.y = c.y+(p1.y-c.y)* d;
p2.x = c.x+(p2.x-c.x)* d;

p2.y = c.y+(p2.y-c.y)* d;
}

void przesun(Punkt2D &p) {

p1.x += p.x; p1.y += p.y;
p2.x += p.x; p2.y += p.y;

}

int rownolegla(Linia2D &l){
double dx1 = l.p2.x- l.p1.x,

dy1 = l.p2.y- l.p1.y,
dx2 = p2.x-p1.x,

dy2 = p2.y-p1.y;
return abs(dx1*dyY2-dy1*dx2)<0.0001;

}

Linia2D lustroOX() {

Linia2D l;
l.p1.x = p1.x; l.p1.y = -p1.y;

l.p2.x = p2.x; l.p2.y = -p2.y;
return l;

}
};

int main() {
Linia2D l1, l2;

Punkt2D p1;

l1.p1.x = l2.p1.x = 0;
l1.p1.y = l2.p1.y = 0;

l1.p2.x = l2.p2.x = 1;
l1.p2.y = l2.p2.y = 1;

p1.x = 1; p1.y = 2;
l2.skaluj(2);

l2.przesun(p1);
cout << l1.rownolegla(l2);

l2 = l2.lustroOX();
return 0;

}

//Geometria.java
import java.lang.*;

class Punkt2D {

public double x, y;
}

class Linia2D {

public Punkt2D p1=new Punkt2D(),
p2=new Punkt2D();

public void skaluj(double d) {

Punkt2D c = new Punkt2D();
c.x = (p1.x+p2.x)/2;

c.y = (p1.y+p2.y)/2;
p1.x = c.x+(p1.x-c.x)* d;

p1.y = c.y+(p1.y-c.y)* d;
p2.x = c.x+(p2.x-c.x)* d;

p2.y = c.y+(p2.y-c.y)* d;
}

public void przesun(Punkt2D p) {

p1.x += p.x; p1.y += p.y;
p2.x += p.x; p2.y += p.y;

}

public boolean rownolegla(Linia2D l) {
double dx1 = l.p2.x- l.p1.x,

dy1 = l.p2.y- l.p1.y,
dx2 = p2.x-p1.x,

dy2 = p2.y-p1.y;
return Math.abs(dx1*dy2-dy1*dx2)<0.0001?

true:false;
}

Linia2D lustroOX() {

Linia2D l = new Linia2D();
l.p1.x = p1.x; l.p1.y = -p1.y;

l.p2.x = p2.x; l.p2.y = -p2.y;
return l;

}
}

public class Geometria {

public static void main(String args[]) {
Linia2D l1 = new Linia2D(),

l2 = new Linia2D();
Punkt2D p1 = new Punkt2D();

l1.p1.x = l2.p1.x = 0;
l1.p1.y = l2.p1.y = 0;

l1.p2.x = l2.p2.x = 1;
l1.p2.y = l2.p2.y = 1;

p1.x = 1; p1.y = 2;
l2.skaluj(2);

l2.przesun(p1);
System.out.println(l1.rownolegla(l2));

l2 = l2.lustroOX();
}

}

1


Wyszukiwarka

Podobne podstrony:
01 Geometry CPP CS
01 Geometry Java CS
01 Geometria analityczna w n wymiarach okładka
wyklad5.cpp, JAVA jest językiem programowania obiektowego
Prolog&Cpp&Java
01-08, Programowanie, ! Java, Java i XML
01 Geometria analityczna w n wymiarach okładka
03 Konstruktory CPP Java
01 Data in Java
01 Geometria analityczna w n wymiarach okładka
01 Data in Java
05 Przykrywanie skladowych CPP Java
Java 01 Wstep

więcej podobnych podstron