Question

In: Computer Science

A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In...

A Point Class Definition

A two-dimensional point may be represented by an x- and y-coordinate. In this problem, you will write a Point class which stores this information and provides useful methods to work with points.

We have provided an __init__ definition for the Point class. You will note that this accepts two arguments, being the x- and y-coordinates of the point.

We have also included a __repr__ method, which will provide a canonical string representation of the point. Make sure you don’t change this method, as we will use it for some tests. Note that the output of __repr__ could be copied into the interpreter in order to create an identical point. This is a characteristic of most good __repr__ methods.

The first thing our Point class should be able to do is determine the distance between points. Write a method dist_to_point, which accepts another instance of the Point class as an argument, and returns the Euclidean distance between the two points. It may be helpful to use math.sqrt.

Using dist_to_point, now write a method is_near which returns whether two points are close. Remember that you can call other methods in a method definition using self, eg self.other_method(arg)

A point is considered ‘close’ if it is within epsilon of another point. We defined epsilon to be a really small number using scientific notation (so, eg, 1e-3 is 1x10^-3 is 0.001

Finally, we want to be able to add two points together. Write a method add_point that adds the position of the Point object given as an argument to the position of self

import math
epsilon = 1e-5


class Point(object):
"""A 2D point in the cartesian plane"""
def __init__(self, x, y):
"""
Construct a point object given the x and y coordinates

Parameters:
x (float): x coordinate in the 2D cartesian plane
y (float): y coordinate in the 2D cartesian plane
"""

self._x = x
self._y = y

def __repr__(self):
return 'Point({}, {})'.format(self._x, self._y)

Solutions

Expert Solution

Thanks for the question. Here is the Point class implementation in Python,

Note : method is_near() returns boolean value

==================================================================================

import math
epsilon=1e-5


class Point(object):

    # initializes the coordinate values
   
def __init__(self,x,y):
        self._x=x
        self._y=y
   
    # prints the two coordinates of the point
   
def __repr__(self):
        return 'Point({},{})'.format(self._x,self._y)

    # function measures the distance between itself and another point
    # passed as argument
  
def dist_to_point(self,point):
        distance = math.sqrt((self._x-point._x)**2 + (self._y-point._y)**2)
        return distance

    # function returns 'True' if the distance between the two points
    # is less than the epsilon value
    # else return 'False'
   
def is_near(self,point):
        distance = self.dist_to_point(point)
        if distance<=epsilon:
            return True
        else
:
            return False

   
# function that adds the point to itself
   
def add_point(self,point):
      self._x+=point._x
        self.y+=point._y


Related Solutions

in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y)...
in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y) in 2-D space. The private data members are the coordinates X and Y of type ‘double’. The public member functions are: A default constructor (no parameters) to initialize X and Y to zero. An explicit value constructor with two parameters to initialize the values of X and Y. Two functions, one to set the X value and the other to set the Y value...
Consider a two-dimensional ideal flow in the x-y plane (with radial coordinate r2 = x2 +...
Consider a two-dimensional ideal flow in the x-y plane (with radial coordinate r2 = x2 + y2). Given the velocity potentials of 1) a uniform flow, 2) a source φ = (q/2π) ln r, and 3) a dipole φ = −d · r/(2πr2): a) Using the principle of superposition, construct a linear combination of the ingredients above that gives the flow past an infinite cylinder. [10 points] b) Sketch the streamlines of the flow everywhere in space. [10 points]
find the x-coordinate of the point, correct to two decimal places, on the parabola y=3.08-x^2 at...
find the x-coordinate of the point, correct to two decimal places, on the parabola y=3.08-x^2 at which the tangent line cuts from the first quadrant the triangle with the smallest area.
In a two-dimensional Cartesian coordinate system the y-component of a given vector is equal to that...
In a two-dimensional Cartesian coordinate system the y-component of a given vector is equal to that vector's magnitude multiplied by which trigonometric function, with respect to the angle between vector and y-axis?
Begin by defining the data type Point that has two coordinate members x and y. Specifically,...
Begin by defining the data type Point that has two coordinate members x and y. Specifically, Point will be implemented as a structure; define operators as needed to satisfy problem requirements. b, prompt the user to input several (x,y) pairs. As the data is entered, store it in a vector of Points called orginal_points. Input is terminated with the EOF character (differs by OS type). To be clear, I am asking you to define an input stream operator to read...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y),...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y), where x and y are integers. Define methods to individually set the x and y coordinates of your new point and retrieve their values. Write an Objective-C program to implement your new class and test it (main section of the file). Your test program needs to create two instances of your class. Make sure your program test prints out the values that you have...
C Language Let us define a Point type to store two-dimensional coordinates (x, y)! Write the...
C Language Let us define a Point type to store two-dimensional coordinates (x, y)! Write the following functions operating on this data type: dist(): calculates the distance between the two points received (using the Pythagorean theorem) equal(): checks if to points are equal or not read(): reads a point from the keyboard and returns it In the main, define two points, and test all these functions. When all tests are passed, solve the following task by utilizing the structure and...
In a rectangular coordinate system, a positive point charge 5.0nC is placed at x=2cm, y=0cm and...
In a rectangular coordinate system, a positive point charge 5.0nC is placed at x=2cm, y=0cm and a negative point charge -5.0nC is placed at x=-2cm,y=0cm. Point P is at x=2cm, y=3cm K=8.99 x 10^9 (Nxm^2)/C^2 a) Calculate the magnitude of the force between the positive charge and negative charge b) Draw the electric field vector at point P, caused by the positive charge. Find its magnitude c) Draw the electric field vector at point P, caused by the negative charge....
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:  ...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:   The data fields x and y that represent the coordinates with gette and setter methods. A no-arg constructor that creates a point (0, 0).   A constructor that constructs a point with specified coordinates. The method equals(GeoPoint p) that returns true if two GeoPoint objects have the same x- and y-coordinates. Write a test program that creates an array of GeoPoint objects. The size of...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should contain: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point at (0, 0). A constructor that creates a point with specified coordinates. Get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point with specified x- and y-coordinates. Use the formula: root (x2-x1)2 +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT