plot two tangent circles in matlab

To plot two tangent circles in MATLAB, you can use the viscircles function with the centers and radii of the circles as inputs. The following code snippet demonstrates how to plot two tangent circles:

main.m
% Define the centers and radii of the two circles
center1 = [0 0];
radius1 = 1;
center2 = [2 0];
radius2 = 1;

% Calculate the distance between the centers
d = norm(center2 - center1);

% Calculate the intersection points of the circles
a = (radius1^2 - radius2^2 + d^2) / (2*d);
h = sqrt(radius1^2 - a^2);
p = center1 + a * (center2 - center1) / d;
intersect1 = p + h * [0 1] * (center2 - center1) / d;
intersect2 = p - h * [0 1] * (center2 - center1) / d;

% Plot the circles and intersection points
figure;
viscircles(center1, radius1, 'Color', 'b');
hold on;
viscircles(center2, radius2, 'Color', 'r');
plot(intersect1(1), intersect1(2), 'ro');
plot(intersect2(1), intersect2(2), 'ro');
axis equal;
703 chars
25 lines

In this code snippet, we define the centers and radii of the two circles using variables center1, radius1, center2, and radius2. We then calculate the distance between the centers, d, and the intersection points of the circles intersect1 and intersect2. Finally, we plot the circles and intersection points using the viscircles function and the plot function. The axis equal command ensures that the plot is displayed with equal axis lengths in both dimensions.

gistlibby LogSnag