Hi friends...
I have a series of points which are actually the points that form a circle... Can anyone help me how to get the circle image when i load these points as input... A simple code example will be helpful...
Thanks in advance....

Dani AI

Generated

— short practical plan, and a small Delphi implementation you can drop into your project.

If your points are unordered, simply connecting them will usually give a messy polygon (as hinted). Two practical choices:

  • If you want the sampled shape drawn exactly: sort points by polar angle around their centroid (use ArcTan2) and draw the polyline or a closed spline.
  • If you want the underlying circle (center + radius): compute a least‑squares circle fit and draw that circle.

The code below implements a robust algebraic least‑squares fit (Kasa style) in Delphi: it returns center (CX,CY) and radius R. It uses double precision and subtracts the centroid for better numeric stability.

type
  TMat3 = array[0..2,0..2] of Double;
  TVec3 = array[0..2] of Double;

function Solve3x3(const M: TMat3; const B: TVec3; out X: TVec3): Boolean;
var
  a: TMat3; b: TVec3; i,j,k,maxi: Integer;
  maxv,factor,tmp: Double;
begin
  a := M; b := B;
  for i := 0 to 2 do
  begin
    maxi := i; maxv := Abs(a[i,i]);
    for j := i+1 to 2 do if Abs(a[j,i]) > maxv then begin maxi := j; maxv := Abs(a[j,i]); end;
    if maxv < 1e-12 then Exit(False);
    if maxi <> i then
    begin
      for k := i to 2 do begin tmp := a[i,k]; a[i,k] := a[maxi,k]; a[maxi,k] := tmp; end;
      tmp := b[i]; b[i] := b[maxi]; b[maxi] := tmp;
    end;
    for j := i+1 to 2 do
    begin
      factor := a[j,i] / a[i,i];
      for k := i to 2 do a[j,k] := a[j,k] - factor * a[i,k];
      b[j] := b[j] - factor * b[i];
    end;
  end;
  for i := 2 downto 0 do
  begin
    if Abs(a[i,i]) < 1e-12 then Exit(False);
    X[i] := b[i];
    for j := i+1 to 2 do X[i] := X[i] - a[i,j] * X[j];
    X[i] := X[i] / a[i,i];
  end;
  Result := True;
end;

function FitCircle(const X, Y: array of Double; out CX, CY, R: Double): Boolean;
var
  n,i: Integer; meanX,meanY,xi,yi,zi: Double;
  Sx,Sy,Sz,Sxx,Sxy,Syy,Sxz,Syz: Double;
  M: TMat3; B,Sol: TVec3; cxs,cys,r2: Double;
begin
  n := Length(X); if (n <> Length(Y)) or (n < 3) then Exit(False);
  meanX := 0; meanY := 0; for i := 0 to n-1 do begin meanX := meanX + X[i]; meanY := meanY + Y[i]; end;
  meanX := meanX / n; meanY := meanY / n;
  Sx := 0; Sy := 0; Sz := 0; Sxx := 0; Sxy := 0; Syy := 0; Sxz := 0; Syz := 0;
  for i := 0 to n-1 do begin
    xi := X[i] - meanX; yi := Y[i] - meanY; zi := xi*xi + yi*yi;
    Sx := Sx + xi; Sy := Sy + yi; Sz := Sz + zi;
    Sxx := Sxx + xi*xi; Sxy := Sxy + xi*yi; Syy := Syy + yi*yi;
    Sxz := Sxz + xi*zi; Syz := Syz + yi*zi;
  end;
  M[0,0]:=Sxx; M[0,1]:=Sxy; M[0,2]:=Sx;
  M[1,0]:=Sxy; M[1,1]:=Syy; M[1,2]:=Sy;
  M[2,0]:=Sx;  M[2,1]:=Sy;  M[2,2]:=n;
  B[0]:=-Sxz; B[1]:=-Syz; B[2]:=-Sz;
  if not Solve3x3(M,B,Sol) then Exit(False);
  cxs := -Sol[0]/2; cys := -Sol[1]/2; r2 := cxs*cxs + cys*cys - Sol[2];
  if (r2 < 0) and (r2 > -1e-9) then r2 := 0;
  if r2 < 0 then Exit(False);
  CX := cxs + meanX; CY := cys + meanY; R := Sqrt(r2);
  Result := True;
end;

Use: call FitCircle with arrays of X/Y; on success draw with Canvas.Ellipse(Round(CX-R),Round(CY-R),Round(CX+R),Round(CY+R)). Troubleshooting: need >=3 non‑colinear points; if fitting fails it usually means degenerate input or strong outliers — filter bad samples or use RANSAC for robustness. Sorting by angle (ArcTan2) fixes unordered samples when you just want to connect the points.

Recommended Answers

All 8 Replies

Do you just want to draw all points that you have ? Connect them by a line ? Or determine the actual circle ?

Actually am expecting that only...That would be great if u get me..When the points are connected it may not be a circle but some random closed figure...

TCanvas has several drawing options, like MoveTo() and LineTo() that you can use.

Is there any example program of similar kind so that i get to know how to use Tcanvas or TBitmap etc....

Thank You very much... I appreciate your help ... It has been very helpful at this moment..

hey can you send me command that will display the co-ordinate points on the Tcanvas...

Form.Canvas.Pixels

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.