# [postgis-users] Section of Circle from 3 Points and Convert to Linestring

Simon Greener simon at spatialdbadvisor.com
Wed Jun 11 18:47:26 PDT 2008

```Eric,

> What a great bunch of GeoSkirmishers you are!
> Here is another question.
>
> I have a line AB where A is (0,0) and B is (10,0).
> I also have a point P that is (5,3).
> I know that the section of the circle that passes through A,P, and B
> can be determined if I know the circle center - point C, which
> can be defined as the intersection of perpendicular lines projected
> from the midpoints of AP and BP.
>
> What is the most economical way of expressing this using PostGIS/PostgreSQL fuctions?
> Alternatively, if there is a better approach, what is that? Thanks!

I don't know if this function is of use to you:

create function FindCircle(p_X1     real, p_Y1 real,
p_X2     real, p_Y2 real,
p_X3     real, p_Y3 real,
OUT p_CX     real,
OUT p_CY     real,
OUT p_Radius real)
As \$\$
Declare
dA          real;
dB          real;
dC          real;
dD          real;
dE          real;
dF          real;
dG          real;
BEGIN
dA := p_X2 - p_X1;
dB := p_Y2 - p_Y1;
dC := p_X3 - p_X1;
dD := p_Y3 - p_Y1;
dE := dA * (p_X1 + p_X2) + dB * (p_Y1 + p_Y2);
dF := dC * (p_X1 + p_X3) + dD * (p_Y1 + p_Y3);
dG := 2.0 * (dA * (p_Y3 - p_Y2) - dB * (p_X3 - p_X2));
-- If dG is zero then the three points are collinear and no finite-radius
-- circle through them exists.
If ( dG = 0 ) Then
p_Radius := -1;
Else
p_CX := (dD * dE - dB * dF) / dG;
p_CY := (dA * dF - dC * dE) / dG;
p_Radius := sqrt(power(p_X1 - p_CX,2) + power(p_Y1 - p_CY,2) );
End If;
end;
\$\$ LANGUAGE plpgsql;

regards
Simon
--
SpatialDB Advice and Design, Solutions Architecture and Programming,
Oracle Database 10g Administrator Certified Associate; Oracle Database 10g SQL Certified Professional
Oracle Spatial, SQL Server, PostGIS, MySQL, ArcSDE, Manifold GIS, Radius Topology and Studio Specialist.
39 Cliff View Drive, Allens Rivulet, 7150, Tasmania, Australia.
Website: www.spatialdbadvisor.com
Email: simon at spatialdbadvisor.com
Voice: +613 9016 3910
Mobile: +61 418 396391
Skype: sggreener
Longitude: 147.20515 (147° 12' 18" E)
Latitude: -43.01530 (43° 00' 55" S)
NAC:W80CK 7SWP3

```

More information about the postgis-users mailing list