comp.soft-sys.matlab
[Top] [All Lists]

Re: generate curves

Subject: Re: generate curves
From: "Michael Garrity"
Date: Fri, 30 Sep 2005 12:04:05 -0400
Newsgroups: comp.soft-sys.matlab
"rags" <RaghuramPR@xxxxxxxxx> wrote in message 
news:1128058863.452798.186120@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> How do i geberate a spline curve passing through three points whose
> space coordinates are given
>
There are a lot of choices for a curve through 3 points. One
simple choice would be a quadratic bezier. The eqn would be

pt = (1-u).^2 * c1 + 2 * u .* (1-u) * c2 + u.^2 * c3;

For a qbezier, c1 & c3 lie on the curve, but c2 doesn't. If
we assume that we'd like the 3rd thru point to lie at u=1/2,
then we can substitute into that equation and get this:

pt(1/2) = 1/4 c1 + 1/2 c2 + 1/4 c3

so:

c2 = 2*pt(1/2) - 1/2*c1 - 1/2*c3

This gives us a function that looks something like this:

function [x y]=qbezier(p1,p2,p3,n)
    u = linspace(0,1,n);
    c1=p1;
    c2 = 2*p2 - p/2 - p/2;
    c3=p3;
    x = [(1-u).^2*c1(1) + 2*u.*(1-u)*c2(1) + u.^2*c3(1)]';
    y = [(1-u).^2*c1(2) + 2*u.*(1-u)*c2(2) + u.^2*c3(2)]';

You can use this like this:

[x y]=qbezier([-1 -1],[0 -2],[1 -1],64),
plot(x,y)

If you play with this, you'll probably find that placing
the middle thru pt at u=1/2 is not optimal in a lot of
cases. You could choose a better parameter by taking
the ratio of (p2-p1) and (p3-p2). I'll leave that as an
exercise for the reader.



There are other spline curves that will give you a lot
more control. For example, if you use some sort of
rational spline, then you can get it to match arc
segments, which can be useful.

    -MPG-




<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy