Skip to content
Snippets Groups Projects
Commit 5078c696 authored by Andreas Schmidt's avatar Andreas Schmidt :seedling:
Browse files

L15: code

parent 961f1142
No related branches found
No related tags found
No related merge requests found
......@@ -18,10 +18,16 @@ double length(Vec2_t *v)
return sqrt(v->x * v->x + v->y * v->y);
}
Vec2_t *newVec2(double x, double y)
{
Vec2_t *v = calloc(1, sizeof(Vec2_t));
v->x = x;
v->y = y;
return v;
}
int main(int argc, char **argv)
{
Vec2_t a;
a.x = 3.0;
a.y = 4.0;
printf("%f\n", length(&a));
Vec2_t *a = newVec2(3.0, 4.0);
printf("%f\n", length(a));
}
public class CartesianVec2 {
public class CartesianVec2 implements Vec2 {
private double x, y;
public CartesianVec2(double x, double y) {
......@@ -14,7 +14,15 @@ public class CartesianVec2 {
return this.y;
}
public void translate(double x, double y) {
public void setX(double v) {
this.x = v;
}
public void setY(double v) {
this.y = v;
}
void translate(double x, double y) {
this.x += x;
this.y += y;
}
......
public class PolarVec2 implements Vec2 {
private double r, phi;
public PolarVec2(double x, double y) {
this.setXandY(x, y);
}
void setXandY(double x, double y) {
this.phi = Math.atan2(y, x);
this.r = Math.sqrt(x * x + y * y);
}
public double getX() {
return this.r * Math.cos(phi);
}
public double getY() {
return this.r * Math.sin(phi);
}
public void setX(double v) {
this.setXandY(v, this.getY());
}
public void setY(double v) {
this.setXandY(this.getX(), v);
}
void translate(double x, double y) {
this.setXandY(this.getX() + x, this.getY() + y);
}
public double length() {
return r;
}
}
\ No newline at end of file
public class Test {
public static void main(String[] args) {
Vec2 v = new Vec2(5, 5);
CartesianVec2 v = new CartesianVec2(3.0, 4.0);
PolarVec2 b = new PolarVec2(4.0, 5.0);
Rectangle rect = new Rectangle();
rect.upperRight = b;
rect.lowerLeft = v;
v.translate(-2, -1);
System.out.println(v.length());
Rectangle r = new Rectangle();
r.lowerLeft = v;
v.translate(2, 2);
r.upperRight = v;
System.out.println(r.area());
// a.translate(-2, 5);
System.out.println(rect.area());
}
}
\ No newline at end of file
public class Vec2 {
private double r, phi;
public interface Vec2 {
void setX(double x);
public Vec2(double x, double y) {
this.setXandY(x, y);
}
void setY(double x);
private void setXandY(double x, double y) {
this.phi = Math.atan2(y, x);
this.r = Math.sqrt(x * x + y * y);
}
double getX();
public double getX() {
return this.r * Math.cos(this.phi);
}
double getY();
public double getY() {
return this.r * Math.sin(this.phi);
}
public void translate(double x, double y) {
this.setXandY(this.getX() + x, this.getY() + y);
}
public double length() {
return this.r;
}
double length();
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment