Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Use python language to answer the three following questions: 1

Use python language to answer the three following questions: 1

Computer Science

Use python language to answer the three following questions:

1. The aim of this exercise is to implement a function for finding when the trajectory of an object is closest to a given point. A point is given by a tuple as a pair of (x,y) coordinates. A trajectory of an object is given by a list of tuples, where each tuple (x,y,t) corresponds to the observed position of the object at a particular time t. Your goal is to write a function that returns the time when the object was observed to be closest to the given point.

 

For example, consider the trajectory [(1,1,1),(2,2,2),(3,1.5,3),(1,1,4)], which contains four observations of the position of an object at times t=1, t=2, t=3 and t=4. If we compare this trajectory to the point (3,2), then the closest observation in the trajectory occurs at time t=3.

 

Write a function closestTime(tr, pt) that takes a list argument tr that corresponds to the trajectory of an object, and a tuple argument pt that corresponds to a test point, and returns the int value of the time of the earliest observation in tr that is closest to the test point pt. Note that if two or more observations are closest to the test point (i.e., they are equally close to the test point, and closer than any other observations), then you should return the time of the earliest such observation.

 

You can assume that the given trajectory contains at least one observation, and that the x and y coordinates in the observation are float values, and the time t of the observation is a non-negative int. You can also assume that the x and y coordinates of the test point are float values. You can assume that the observations in the trajectory are ordered by increasing time values.

 

You should calculate the distance between an observation and the test point using the Euclidean distance between two points (x1,y1) and (x2,y2):

d(x1,y1,x2,y2)=(x1x2)2+(y1y2)2−−−−−−−−−−−−−−−−−−

Example calls to the function are:

 

 

>>> print(closestTime([(0,0,0),(1,1,1),(2,2,2),(1,1,3)], (2,2)))

2

>>> print(closestTime([(0,0,0),(1,1,1),(2,2,2),(1,1,3)], (1.2,1.1)))

1

>>> print(closestTime([(0,0,2),(1,1,3),(2,2,4),(1,1,5)], (-2,-2)))

2

>>> print(closestTime([(0,0,0)], (-2,-2)))

0

 

 

2. Your aim is to count the number of times a given trajectory enters a given rectangular region in space (which we refer to as a query box). You are given a trajectory and a query box. The trajectory is in the same format as the previous problem. You can assume that the observations in the trajectory are ordered by increasing time values. The query box is given as a tuple that contains two tuples, i.e., ((x1,y1),(x2,y2)), where the first tuple corresponds to the lower-left corner of the rectangular region, and the second tuple corresponds to the upper-right corner of the rectangular region.

 

You can assume that if a trajectory observation falls on the boundary of the box, it is considered as being inside the box. For example, the observation (1,1,0) is considered to be inside the box ((1,1),(2,2)). Note that in the special case where the trajectory starts within the query box, then that also counts as an instance of entering the box.

 

For example, for a trajectory containing five observations [(1,1,0),(2,2,1),(3,3,2),(2,2,3),(1,1,4)], it enters the query box ((1.5,1.5),(2.5,2.5)) twice. For the same trajectory [(1,1,0),(2,2,1),(3,3,2),(2,2,3),(1,1,4)] but a different query box ((0.5,0.5),(1.5,1.5)), it also enters the query box twice.

 

Write a function visits(tr, b) that takes a list argument tr corresponding to a trajectory, and a tuple b corresponding to a rectangular query box, and returns an int corresponding to the number of times the trajectory enters the box. You can assume that the list argument tr contains at least one observation.

 

Example calls to the function are:

 

 

>>> print(visits([(1,1,0),(2,2,1),(3,3,2),(2,2,3),(1,1,4)], ((1.5,1.5),(2.5,2.5))))

2

>>> print(visits([(1,1,0),(2,2,1),(3,3,2),(2,2,3),(1,1,4)], ((1.5,1.5),(1.6,1.6))))

0

>>> print(visits([(1,1,0),(2,2,1),(3,3,2),(2,2,3),(1,1,4)], ((0,0),(3,3))))

1

>>> print(visits([(1,3,2),(3,3,3),(3,1,4)], ((0,0),(2,2))))

0

 

 

The next trajectory analysis function that you will implement compares two trajectories to see whether two objects are moving together. We can do this by comparing observations that occur at the same time in each trajectory, and checking if their distance is less than or equal to a given threshold. If so, we consider those observations to be close. The aim is to find the maximum number of consecutive observations in the two trajectories, such that each pair of observations occurred at the same time and were close together.

 

Write a function together(tr1, tr2, th) that takes two list arguments tr1, tr2 (each corresponding to a trajectory), and a float argument th (corresponding to the threshold used to test if two observations are close), and returns the int count of the maximum number of consecutive observations in each trajectory such that the observations occur at the same time and are close.

 

You can assume that the list arguments tr1, tr2 each contain at least one observation, that the observations in each trajectory are ordered by increasing time values, and that there are no missing time values between the start and end times of a trajectory. Note that the given trajectories tr1, tr2 can have different start and/or end times. You can also assume that the threshold th is greater than zero. Note that you will need to calculate the Euclidean distance between two points in order to determine if they are close.

 

Example calls to the function are:

 

 

>>>print(together([(0,0,0),(1,1,1),(2,2,2),(1,1,3),(1,1,4)],[(0,0,0),(6,1,1),(2,4,2),(1,1,3),(1,0.7,4)], 0.5))

2

>>>print(together([(0,0,0),(1,1,1),(2,2,2),(1,1,3),(1,1,4)],[(0,0,0),(6,1,1),(2,4,2),(1,1,3),(1,0.7,4)], 0.1))

1

>>> print(together([(0,0,2),(1,1,3),(2,2,4),(1,1,5)], [(0,0,0),(1,1,1),(2,2,2)], 0.5))

0

>>> print(together([(0,0,0)], [(0,0,1)], 0.5))

0

Option 1

Low Cost Option
Download this past answer in few clicks

9.99 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE

Related Questions