import math
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __str__(self):
return f"({self.x}, {self.y})"
def __eq__(self, other):
if type(other) != type(self):
return False
# I know both are Points!
return self.x == other.x and self.y == other.y
def reflection_across_X(self):
return Point(self.x, -self.y)
def distance_to(self, other):
return math.hypot(self.x - other.x, self.y - other.y)
def main():
p = Point(3,4)
print(p)
origin = Point()
print(p.reflection_across_X())
print(p.distance_to(origin))
main()
public class Point
{
private final int x;
private final int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public Point()
{
this(0,0); //calls sibling constructor
}
public Point reflectionAcrossX()
{
return new Point(x, -y);
}
@Override
public String toString()
{
return String.format("(%s, %s)", x, y);
}
public double distanceTo(Point that)
{
return Math.hypot(x - that.x, y - that.y);
}
public static void main(String[] args)
{
Point p = new Point(3,4);
System.out.println(p);
System.out.println(p.reflectionAcrossX());
Point origin = new Point();
System.out.println(p.distanceTo(origin));
}
}
public class Driver
{
public static void main(String[] args)
{
Point p = new Point();
p.x = 3;
}
}
Add Methods
Quadrants in the Plane In case you forgot...
| | Quadrant 2 | Quadrant 1 | | | ---------------------------------------- | | Quadrant 3 | Quadrant 4 | | |
Hint: Here is Java's if
scheme next to
Python's. The rules are the same; the primary difference is
purely cosmetic. This will help you with the quadrant problem.
if predicate0: if(predicate0) block0 { block0; } elif predicate1: else if (predicate1) block 1 { block1; } else: else default { default; }
Useful hints for the last problem
Find the lengths of the three sides.
Use Herron's formula. If a triangle has side lengths \(a\), \(b\), and \(c\), define the semiperimeter by
$$s = {a + b + c\over 2}.$$The area contained in the triangle is
$$ A = \sqrt{s(s-a)(s-b)(s-c)}.$$Python | Java | Description |
---|---|---|
reflection_across_Y(self) |
Point reflectionAcrossY() |
returns the reflection of this point across the y-axis |
reflection_through_origin(self) |
Point reflectionThroughOrigin() |
returns the reflection of this point through the origin |
rotate_90(self) |
Point rotate90() |
returns the Point obtained by rotating this point about the origin 90 degrees |
above_the_line(self, m, b) |
Point aboveTheLine() |
returns true if the this point is above the line y = mx + b (false if it is on the line or below) |
quadrant(self) |
int quadrant() |
returns 0 if the point lies on an axis; otherwise it returns the quadrant of the plane the point lies in (1-4) |
area_of_triangle(self, p, q) |
int areaOfTriangle(p, q) |
Finds the area of the triangle bounded by this Point, p, and q. |