Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to draw an arc between two points given centre and radius

Subject: how to draw an arc between two points given centre and radius

From: Chanpreet

Date: 30 Mar, 2010 19:19:05

Message: 1 of 9

how to draw an arc between two points given centre and radius on matlab

Subject: how to draw an arc between two points given centre and radius

From: Walter Roberson

Date: 30 Mar, 2010 19:41:08

Message: 2 of 9

Chanpreet wrote:
> how to draw an arc between two points given centre and radius on matlab

If you are given the two points, and given the centre and radius, then you
cannot always do that. The distance from the centre and each of the points
define radii, and if those two radii are not equal or are not equal to the
given radius, then you cannot draw a circular arc.

The circular arcs that pass through any two given points define a line. If the
centre is not on that line, you cannot do what you ask. Only one coordinate of
the centre would need to be given, and the radius would not be needed. If,
alternately, the radius were given, then that would narrow it to one of
exactly two points, and the only reason the centre would need to be given
would be to distinguish which of the two points it was.

Did you have something different in mind, such as a non-circular arc? Perhaps
a parabolic arc, and the centre is really the focus?


Anyhow...


You can use arctan to find the sector angle to be drawn. Be sure to check in
case you are crossing over the periodic boundary, as you probably want to draw
the smaller of the two arcs. Then, knowing the angles involved, linspace() the
angle range into an appropriate number of points, and then the points of the
arc are

[radius * cos(theta_range) + x of centre, radius * sin(theta_range) + y of
centre)]

Subject: how to draw an arc between two points given centre and radius

From: Roger Stafford

Date: 30 Mar, 2010 23:02:21

Message: 3 of 9

"Chanpreet " <c.kaur@tue.nl> wrote in message <hotir8$8a8$1@fred.mathworks.com>...
> how to draw an arc between two points given centre and radius on matlab

  Assuming you are in two dimensions, let your two points be the column vectors P1 = [x1;y1], and P2 = [x2;y2], and let the center be P0 = [x0;y0]. Then an arc between P1 and P2 with P0 as its center can be drawn by:

 n = 1000; % The number of points in the arc
 v1 = P1-P0;
 v2 = P2-P0;
 c = det([v1,v2]); % "cross product" of v1 and v2
 a = linspace(0,atan2(abs(c),dot(v1,v2)),n); % Angle range
 v3 = [0,-c;c,0]*v1; % v3 lies in plane of v1 and v2 and is orthog. to v1
 v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a); % Arc, center at (0,0)
 plot(v(1,:)+x0,v(2,:)+y0,'y.') % Plot arc, centered at P0
 axis equal

  Note that it is assumed here that P2 is the same distance from P0 as is P1. If not, the arc will miss P2, though its endpoint will be on the line through P0 and P2.

Roger Stafford

Subject: how to draw an arc between two points given centre and radius

From: Roger Stafford

Date: 31 Mar, 2010 01:13:07

Message: 4 of 9

> "Chanpreet " <c.kaur@tue.nl> wrote in message <hotir8$8a8$1@fred.mathworks.com>...
> > how to draw an arc between two points given centre and radius on matlab

  I should have added that if you are working with three dimensional space rather than two, a method of generating the arc was given in a thread yesterday at:

 http://www.mathworks.com/matlabcentral/newsreader/view_thread/277881

Roger Stafford

Subject: how to draw an arc between two points given centre and radius

From: Brian

Date: 2 Apr, 2012 22:55:15

Message: 5 of 9

"Roger Stafford" wrote in message <hotvtt$gdr$1@fred.mathworks.com>...
> v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a); % Arc, center at (0,0)

Excellent, elegant routine. Thanks for your effort.

But, the calculation fails if all three points are on the same line, as when drawing a semicircle. The calculation above results in a NaN for each element of v1. Any suggestions?

-Brian Callahan

Subject: how to draw an arc between two points given centre and radius

From: ImageAnalyst

Date: 2 Apr, 2012 23:24:28

Message: 6 of 9

On Apr 2, 6:55 pm, "Brian " <br1...@hotmail.com> wrote:
> "Roger Stafford" wrote in message <hotvtt$gd...@fred.mathworks.com>...
> >  v  = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a); % Arc, center at (0,0)
>
> Excellent, elegant routine. Thanks for your effort.
>
> But, the calculation fails if all three points are on the same line, as when drawing a semicircle. The calculation above results in a NaN for each element of v1. Any suggestions?
>
> -Brian Callahan

---------------------------------------------------------------
Exactly which 3 points are you drawing when you draw your
semicircle??? On the line that cuts across the diameter? That's the
only straight line. No points on the arc are in a line unless it's of
infinite radius.

Subject: how to draw an arc between two points given centre and radius

From: Brian

Date: 3 Apr, 2012 00:10:11

Message: 7 of 9

What about changing the statement to add this conditional?

 if c==0 % semicircle
    v3=[0 -1;1 0]*v1; % v1 rotated 90 degrees CCW
 else
    v3 = [0,-c;c,0]*v1; % v3 lies in plane of v1 and v2 and is orthog. to v1
 end

This results in a counterclockwise semicircle from P1 around the center point. If you want the semicircle the other way, then swap P1 and P2.

This approach does not allow "missing" the P2 point and drawing the semicircle clockwise, though.

Subject: how to draw an arc between two points given centre and radius

From: Brian

Date: 3 Apr, 2012 00:20:12

Message: 8 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0e6f9cf3-a0f0-4e69-bc5d-9207ae4d8e18@z17g2000yqf.googlegroups.com>...
> Exactly which 3 points are you drawing when you draw your
> semicircle??? On the line that cuts across the diameter? That's the
> only straight line. No points on the arc are in a line unless it's of
> infinite radius.

The original poster requested a routine intended to draw an arc from a start point to an end point around a center point, not an arc through three points...

Subject: how to draw an arc between two points given centre and radius

From: Roger Stafford

Date: 3 Apr, 2012 01:27:10

Message: 9 of 9

"Brian" wrote in message <jldf53$il7$1@newscl01ah.mathworks.com>...
> What about changing the statement to add this conditional?
>
> if c==0 % semicircle
> v3=[0 -1;1 0]*v1; % v1 rotated 90 degrees CCW
> else
> v3 = [0,-c;c,0]*v1; % v3 lies in plane of v1 and v2 and is orthog. to v1
> end
>
> This results in a counterclockwise semicircle from P1 around the center point. If you want the semicircle the other way, then swap P1 and P2.
>
> This approach does not allow "missing" the P2 point and drawing the semicircle clockwise, though.
- - - - - - - - -
  In 2D as the points approach a straight line the problem becomes computationally unstable, which reflects the growing uncertainty as to which of two arcs is to be followed. In 3D, it would be a particularly ill-defined problem since there are infinitely many possible arcs in that case.

  (By the way, v1 does not become NaNs. It is v3/norm(v3) that makes v have NaNs.)

  If it is understood that the arc is to be unconditionally drawn counterclockwise from P1 with P0 as center until reaching P2 no matter which way P2 points, you would do this:

 v1 = P1-P0;
 v2 = P2-P0;
 v3 = [0 -1;1 0]*v1; % (as you suggested)
 a = linspace(0,mod(atan2(det([v1,v2]),dot(v1,v2)),2*pi));
 % Note the absence of the 'abs' function in 'atan2'
 v = v1*cos(a)+v3*sin(a);
 plot(v(1,:)+P0(1),v(2,:)+P0(2),'y.')
 axis equal

Here the angle 'a' can range anywhere from 0 to 2*pi.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us