How to resolve the algorithm Circles of given radius through two points step by step in the Tcl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Circles of given radius through two points step by step in the Tcl programming language

Table of Contents

Problem Statement

Given two points on a plane and a radius, usually two circles of given radius can be drawn through the points.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Circles of given radius through two points step by step in the Tcl programming language

Source code in the tcl programming language

proc findCircles {p1 p2 r} {
    lassign $p1 x1 y1
    lassign $p2 x2 y2
    # Special case: coincident & zero size
    if {$x1 == $x2 && $y1 == $y2 && $r == 0.0} {
	return [list [list $x1 $y1 0.0]]
    }
    if {$r <= 0.0} {
	error "radius must be positive for sane results"
    }
    if {$x1 == $x2 && $y1 == $y2} {
	error "no sane solution: points are coincident"
    }

    # Calculate distance apart and separation vector
    set dx [expr {$x2 - $x1}]
    set dy [expr {$y2 - $y1}]
    set q [expr {hypot($dx, $dy)}]
    if {$q > 2*$r} {
	error "no solution: points are further apart than required diameter"
    }

    # Calculate midpoint
    set x3 [expr {($x1+$x2)/2.0}]
    set y3 [expr {($y1+$y2)/2.0}]
    # Fractional distance along the mirror line
    set f [expr {($r**2 - ($q/2.0)**2)**0.5 / $q}]
    # The two answers
    set c1 [list [expr {$x3 - $f*$dy}] [expr {$y3 + $f*$dx}] $r]
    set c2 [list [expr {$x3 + $f*$dy}] [expr {$y3 - $f*$dx}] $r]
    return [list $c1 $c2]
}


foreach {p1 p2 r} {
    {0.1234 0.9876} {0.8765 0.2345} 2.0
    {0.0000 2.0000} {0.0000 0.0000} 1.0
    {0.1234 0.9876} {0.1234 0.9876} 2.0
    {0.1234 0.9876} {0.8765 0.2345} 0.5
    {0.1234 0.9876} {0.1234 0.9876} 0.0
} {
    puts "p1:([join $p1 {, }]) p2:([join $p2 {, }]) r:$r =>"
    if {[catch {
	foreach c [findCircles $p1 $p2 $r] {
	    puts "\tCircle:([join $c {, }])"
	}
    } msg]} {
	puts "\tERROR: $msg"
    }
}


  

You may also check:How to resolve the algorithm Unicode strings step by step in the Zig programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Crystal programming language
You may also check:How to resolve the algorithm Number names step by step in the XPL0 programming language