This is in MATLAB: Given a circle whose center is at (a, b) and whose radius is
ID: 671075 • Letter: T
Question
This is in MATLAB:
Given a circle whose center is at (a, b) and whose radius is r, we can determine whether a point (x, y) is inside, on, or outside the circle by finding the distance between the center and the point. If the difference between the radius and the distance is positive, the point is inside the circle; if the difference is negative, the point is outside the circle; if the difference is 0, the point is on the circle. Define a function named insideCircle that takes 3 arguments as input: a point, the center of the circle, and the radius of the circle. The first line of the function should look like: function result = insideCircle(point, center, radius) You may assume that center is a 1 x 2 vector containing the x- and y-coordinates of the center of the circle and that point is a 1 x 2 vector containing the x- and y-coordinates of a point. The function should return 1 if the point is inside the circle, 0 if the point is on the circle, and -1 if the point is outside the circle. Tip: use the sign function (type help sign at the command prompt to find out about it). Demonstrate that your function works by adding code to your main homework script to call your function using a circle with center (2, 4) and radius 3 and the data points (2, 7), (4, 3), and (5, 6) (i.e., there should be three separate function calls). Make sure to display the result of each function call (e.g., by leaving it unsuppressed). Note: this is the same circle and data points that you plotted in problem 3.
Explanation / Answer
function result = insideCircle(point, center, radius)
%find the distance b/w two points
%d = sqrt( (x2-x1)^2+ (y2-y1)^2 )
xdif2 = power(point(1)-center(1), 2);
ydif2 = power(point(2)-center(2), 2);
distance = sqrt(xdif2+ydif2);
result = sign(radius-distance);
end
%ircle with center (2, 4) and radius 3 and the data points (2, 7), (4, 3), and (5, 6)
center = [2 4];
radius = 3;
result = insideCircle([2 7],center,radius);
disp(result);
result = insideCircle([4 3],center,radius);
disp(result)
result = insideCircle([5 6],center,radius);
disp(result)
---------------------ouput----------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.