Golf the smallest circle!Area of a 2D convex hullCircle Through Three PointsThe centers of a triangleCheck if...
Going to get married soon, should I do it on Dec 31 or Jan 1?
Does ultrasonic bath cleaning damage laboratory volumetric glassware calibration?
SPI Waveform on Raspberry Pi Not clean and I'm wondering why
Why is the divergence of this series apparently not predicted by the Monotonic Sequence Theorem?
Why does this function call behave sensibly after calling it through a typecasted function pointer?
Are there any vegetarian astronauts?
“Faire” being used to mean “avoir l’air”?
How would a order of Monks that renounce their names communicate effectively?
Cross over of arrows in a complex diagram
Was touching your nose a greeting in second millenium Mesopotamia?
Transitive action of a discrete group on a compact space
Intuitively, why does putting capacitors in series decrease the equivalent capacitance?
How to modify the uneven space between separate loop cuts, while they are already cut?
Row to remove the dotted white border around focused button text
Difference between 'demás' and 'otros'?
A player is constantly pestering me about rules, what do I do as a DM?
Confusion about multiple information Sets
How come I was asked by a CBP officer why I was in the US when leaving?
Generate and graph the Recamán Sequence
What's the point of DHS warning passengers about Manila airport?
How exactly is a normal force exerted, at the molecular level?
Set vertical spacing between two particular items
Why does the numerical solution of an ODE move away from an unstable equilibrium?
How hard is it to sell a home which is currently mortgaged?
Golf the smallest circle!
Area of a 2D convex hullCircle Through Three PointsThe centers of a triangleCheck if point lies inside triangleOverlapping circleFinding Exclusive Area in Circle IntersectionsText on a circleDoes the triangle contain the origin?Integer triangles with perimeter less than nSmallest Integer DiskArea of a 2D convex hull
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
The problem:
Given a non-empty set of points in the cartesian plane, find the smallest circle that encloses them all (Wikipedia link).
This problem is trivial if the number of points is three or less (if there's one point, the circle has a radius of zero; if there are two points, the line segment that joins the points is the diameter of the circle; if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all if they form a non-obtude triangle, or a circle that touches only two points and encloses the third if the triangle is obtuse). So, for the sake of this challenge, the number of points should be greater than three.
The challenge:
Input: A list of 4 or more non colinear points. The points should have X and Y coordinates; coordinates can be floats. To ease the challenge, no two points should share the same X coordinate.
For example:[(0,0), (2,1), (5,3), (-1,-1)]
Output: A tuple of values,(h,k,r)
, such that $(x-h)^2 + (y-k)^2 = r^2$ is the equation of the smallest circle that encloses all points.
Rules:
- You can choose whatever input method suits your program.
- Output should be printed to
STDOUT
or returned by a function. - "Normal", general-purpose, languages are preferred, but any esolang is acceptable.
- You can assume that the points are not colinear.
- Output can be rounded to 2 decimal position (if that eases your work).
- This is code-golf, so the smallest program in bytes wins. The winner will be selected one week after the challenge is posted.
- Please include the language you used and the length in bytes as header in the first line of your answer:
# Language: n bytes
- Please include the language you used and the length in bytes as header in the first line of your answer:
Test cases:
- 1:
- Input:
[(-8,0), (3,1), (-6.2,-8), (3,9.5)]
- Output:
[-1.6, 0.75, 9.89]
- Input:
- 2:
- Input:
[(7.1,-6.9), (-7,-9), (5,10), (-9.5,-8)]
- Output:
[1.65, 0.52, 11.58]
- Input:
- 3:
- Input:
[(0,0), (1,2), (3,-4), (4,-5), (10,-10)]
- Output:
[5.5, -4.7, 7.5]
- Input:
- 4:
- Input:
[(6,6), (-6,7), (-7,-6), (6,-8)]
- Output:
[0, -0.5, 9.60]
- Input:
Happy golfing!!!
Related challenge:
- Area of a 2D convex hull
code-golf geometry
$endgroup$
add a comment |
$begingroup$
The problem:
Given a non-empty set of points in the cartesian plane, find the smallest circle that encloses them all (Wikipedia link).
This problem is trivial if the number of points is three or less (if there's one point, the circle has a radius of zero; if there are two points, the line segment that joins the points is the diameter of the circle; if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all if they form a non-obtude triangle, or a circle that touches only two points and encloses the third if the triangle is obtuse). So, for the sake of this challenge, the number of points should be greater than three.
The challenge:
Input: A list of 4 or more non colinear points. The points should have X and Y coordinates; coordinates can be floats. To ease the challenge, no two points should share the same X coordinate.
For example:[(0,0), (2,1), (5,3), (-1,-1)]
Output: A tuple of values,(h,k,r)
, such that $(x-h)^2 + (y-k)^2 = r^2$ is the equation of the smallest circle that encloses all points.
Rules:
- You can choose whatever input method suits your program.
- Output should be printed to
STDOUT
or returned by a function. - "Normal", general-purpose, languages are preferred, but any esolang is acceptable.
- You can assume that the points are not colinear.
- Output can be rounded to 2 decimal position (if that eases your work).
- This is code-golf, so the smallest program in bytes wins. The winner will be selected one week after the challenge is posted.
- Please include the language you used and the length in bytes as header in the first line of your answer:
# Language: n bytes
- Please include the language you used and the length in bytes as header in the first line of your answer:
Test cases:
- 1:
- Input:
[(-8,0), (3,1), (-6.2,-8), (3,9.5)]
- Output:
[-1.6, 0.75, 9.89]
- Input:
- 2:
- Input:
[(7.1,-6.9), (-7,-9), (5,10), (-9.5,-8)]
- Output:
[1.65, 0.52, 11.58]
- Input:
- 3:
- Input:
[(0,0), (1,2), (3,-4), (4,-5), (10,-10)]
- Output:
[5.5, -4.7, 7.5]
- Input:
- 4:
- Input:
[(6,6), (-6,7), (-7,-6), (6,-8)]
- Output:
[0, -0.5, 9.60]
- Input:
Happy golfing!!!
Related challenge:
- Area of a 2D convex hull
code-golf geometry
$endgroup$
$begingroup$
Link to sandbox post
$endgroup$
– Barranka
8 hours ago
1
$begingroup$
Is anyone else getting the same radius but with different coordinates for the 2nd and 3rd test cases?
$endgroup$
– Arnauld
6 hours ago
1
$begingroup$
“if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all”—but that may not be the smallest enclosing circle. For the three vertices of an obtuse triangle, the smallest enclosing circle is the one whose diameter is the longest side.
$endgroup$
– Anders Kaseorg
5 hours ago
1
$begingroup$
@Arnauld Same as you. For test case 2, I get center (-1.73, 0.58) and for test case 3 (5.5, -4).
$endgroup$
– Robin Ryder
4 hours ago
$begingroup$
@Arnauld thanks for your comment. I have edited the post accordingly
$endgroup$
– Barranka
2 hours ago
add a comment |
$begingroup$
The problem:
Given a non-empty set of points in the cartesian plane, find the smallest circle that encloses them all (Wikipedia link).
This problem is trivial if the number of points is three or less (if there's one point, the circle has a radius of zero; if there are two points, the line segment that joins the points is the diameter of the circle; if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all if they form a non-obtude triangle, or a circle that touches only two points and encloses the third if the triangle is obtuse). So, for the sake of this challenge, the number of points should be greater than three.
The challenge:
Input: A list of 4 or more non colinear points. The points should have X and Y coordinates; coordinates can be floats. To ease the challenge, no two points should share the same X coordinate.
For example:[(0,0), (2,1), (5,3), (-1,-1)]
Output: A tuple of values,(h,k,r)
, such that $(x-h)^2 + (y-k)^2 = r^2$ is the equation of the smallest circle that encloses all points.
Rules:
- You can choose whatever input method suits your program.
- Output should be printed to
STDOUT
or returned by a function. - "Normal", general-purpose, languages are preferred, but any esolang is acceptable.
- You can assume that the points are not colinear.
- Output can be rounded to 2 decimal position (if that eases your work).
- This is code-golf, so the smallest program in bytes wins. The winner will be selected one week after the challenge is posted.
- Please include the language you used and the length in bytes as header in the first line of your answer:
# Language: n bytes
- Please include the language you used and the length in bytes as header in the first line of your answer:
Test cases:
- 1:
- Input:
[(-8,0), (3,1), (-6.2,-8), (3,9.5)]
- Output:
[-1.6, 0.75, 9.89]
- Input:
- 2:
- Input:
[(7.1,-6.9), (-7,-9), (5,10), (-9.5,-8)]
- Output:
[1.65, 0.52, 11.58]
- Input:
- 3:
- Input:
[(0,0), (1,2), (3,-4), (4,-5), (10,-10)]
- Output:
[5.5, -4.7, 7.5]
- Input:
- 4:
- Input:
[(6,6), (-6,7), (-7,-6), (6,-8)]
- Output:
[0, -0.5, 9.60]
- Input:
Happy golfing!!!
Related challenge:
- Area of a 2D convex hull
code-golf geometry
$endgroup$
The problem:
Given a non-empty set of points in the cartesian plane, find the smallest circle that encloses them all (Wikipedia link).
This problem is trivial if the number of points is three or less (if there's one point, the circle has a radius of zero; if there are two points, the line segment that joins the points is the diameter of the circle; if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all if they form a non-obtude triangle, or a circle that touches only two points and encloses the third if the triangle is obtuse). So, for the sake of this challenge, the number of points should be greater than three.
The challenge:
Input: A list of 4 or more non colinear points. The points should have X and Y coordinates; coordinates can be floats. To ease the challenge, no two points should share the same X coordinate.
For example:[(0,0), (2,1), (5,3), (-1,-1)]
Output: A tuple of values,(h,k,r)
, such that $(x-h)^2 + (y-k)^2 = r^2$ is the equation of the smallest circle that encloses all points.
Rules:
- You can choose whatever input method suits your program.
- Output should be printed to
STDOUT
or returned by a function. - "Normal", general-purpose, languages are preferred, but any esolang is acceptable.
- You can assume that the points are not colinear.
- Output can be rounded to 2 decimal position (if that eases your work).
- This is code-golf, so the smallest program in bytes wins. The winner will be selected one week after the challenge is posted.
- Please include the language you used and the length in bytes as header in the first line of your answer:
# Language: n bytes
- Please include the language you used and the length in bytes as header in the first line of your answer:
Test cases:
- 1:
- Input:
[(-8,0), (3,1), (-6.2,-8), (3,9.5)]
- Output:
[-1.6, 0.75, 9.89]
- Input:
- 2:
- Input:
[(7.1,-6.9), (-7,-9), (5,10), (-9.5,-8)]
- Output:
[1.65, 0.52, 11.58]
- Input:
- 3:
- Input:
[(0,0), (1,2), (3,-4), (4,-5), (10,-10)]
- Output:
[5.5, -4.7, 7.5]
- Input:
- 4:
- Input:
[(6,6), (-6,7), (-7,-6), (6,-8)]
- Output:
[0, -0.5, 9.60]
- Input:
Happy golfing!!!
Related challenge:
- Area of a 2D convex hull
code-golf geometry
code-golf geometry
edited 2 hours ago
Barranka
asked 8 hours ago
BarrankaBarranka
1668 bronze badges
1668 bronze badges
$begingroup$
Link to sandbox post
$endgroup$
– Barranka
8 hours ago
1
$begingroup$
Is anyone else getting the same radius but with different coordinates for the 2nd and 3rd test cases?
$endgroup$
– Arnauld
6 hours ago
1
$begingroup$
“if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all”—but that may not be the smallest enclosing circle. For the three vertices of an obtuse triangle, the smallest enclosing circle is the one whose diameter is the longest side.
$endgroup$
– Anders Kaseorg
5 hours ago
1
$begingroup$
@Arnauld Same as you. For test case 2, I get center (-1.73, 0.58) and for test case 3 (5.5, -4).
$endgroup$
– Robin Ryder
4 hours ago
$begingroup$
@Arnauld thanks for your comment. I have edited the post accordingly
$endgroup$
– Barranka
2 hours ago
add a comment |
$begingroup$
Link to sandbox post
$endgroup$
– Barranka
8 hours ago
1
$begingroup$
Is anyone else getting the same radius but with different coordinates for the 2nd and 3rd test cases?
$endgroup$
– Arnauld
6 hours ago
1
$begingroup$
“if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all”—but that may not be the smallest enclosing circle. For the three vertices of an obtuse triangle, the smallest enclosing circle is the one whose diameter is the longest side.
$endgroup$
– Anders Kaseorg
5 hours ago
1
$begingroup$
@Arnauld Same as you. For test case 2, I get center (-1.73, 0.58) and for test case 3 (5.5, -4).
$endgroup$
– Robin Ryder
4 hours ago
$begingroup$
@Arnauld thanks for your comment. I have edited the post accordingly
$endgroup$
– Barranka
2 hours ago
$begingroup$
Link to sandbox post
$endgroup$
– Barranka
8 hours ago
$begingroup$
Link to sandbox post
$endgroup$
– Barranka
8 hours ago
1
1
$begingroup$
Is anyone else getting the same radius but with different coordinates for the 2nd and 3rd test cases?
$endgroup$
– Arnauld
6 hours ago
$begingroup$
Is anyone else getting the same radius but with different coordinates for the 2nd and 3rd test cases?
$endgroup$
– Arnauld
6 hours ago
1
1
$begingroup$
“if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all”—but that may not be the smallest enclosing circle. For the three vertices of an obtuse triangle, the smallest enclosing circle is the one whose diameter is the longest side.
$endgroup$
– Anders Kaseorg
5 hours ago
$begingroup$
“if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all”—but that may not be the smallest enclosing circle. For the three vertices of an obtuse triangle, the smallest enclosing circle is the one whose diameter is the longest side.
$endgroup$
– Anders Kaseorg
5 hours ago
1
1
$begingroup$
@Arnauld Same as you. For test case 2, I get center (-1.73, 0.58) and for test case 3 (5.5, -4).
$endgroup$
– Robin Ryder
4 hours ago
$begingroup$
@Arnauld Same as you. For test case 2, I get center (-1.73, 0.58) and for test case 3 (5.5, -4).
$endgroup$
– Robin Ryder
4 hours ago
$begingroup$
@Arnauld thanks for your comment. I have edited the post accordingly
$endgroup$
– Barranka
2 hours ago
$begingroup$
@Arnauld thanks for your comment. I have edited the post accordingly
$endgroup$
– Barranka
2 hours ago
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
Wolfram Language (Mathematica), 28 bytes
BoundingRegion[#,"MinDisk"]&
Try it online!
Built-ins are handy here. Output is a disk object with the centre and radius. Like others, I’ve found the 2nd and 3rd test cases to be different to the question.
$endgroup$
1
$begingroup$
I was expecting a Mathematica built-in. But I wasn't expecting Mathematica to have "disk objects".
$endgroup$
– Robin Ryder
4 hours ago
add a comment |
$begingroup$
R, 67 bytes
function(x,a=nlm(function(y)max(Mod(x-y%*%c(1,1i))),0:1))c(a$e,a$m)
Try it online!
Takes input as a vector of complex coordinates. Mod
is the distance (modulus) in the complex plane and nlm
is an optimization function: it finds the position of the center (a$e
) which minimizes the maximum distance to the input points, and gives the corresponding distance (a$m
), i.e. the radius. Accurate to 3-6 digits; the TIO footer rounds the output to 2 digits.
nlm
takes a numeric vector as input: the y%*%c(1,1i)
business converts it to a complex.
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 298 bytes
Returns an array [x, y, r]
.
p=>p.map(m=h=>p.map(i=>[0,...p].map(j=>!p.every(([x,y])=>H(x-X,y-Y)<=r,([a,A]=h,[b,B]=i),X=j?x=([c,C]=j,P=a*a+A*A,Q=b*b+B*B,R=c*c+C*C,d=c*(l=A-B)+a*(B-=C)+b*(C-=A),P*B+Q*C+R*l)/d/2:(a/=2)+(x=b/2),Y=j?y=(P*(c-b)+Q*(a-c)+R*(b-a))/d/2:(A/=2)+(y=B/2),H=Math.hypot,r=H(x-a,y-A))|r>m||(o=[X,Y,m=r]))))&&o
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 100 bytes
ZÆm,Hñ/$
_²§½
1ịṭƊZIṚṙ€1N1¦,@ṭ@²§$µḢZḢ×Ø1œị$SḤ÷@P§
ZṀ+ṂHƲ€_@Ç+ḷʋ⁸,_²§½ʋḢ¥¥
œc3Ç€;ŒcÑ€$2ŀ_ƭƒⱮṀ¥Ðḟ⁸ṚÞḢ
Try it online!
In contrast to my Wolfram language answer, Jelly needs quite a lot of code to achieve this (unless I’m missing something!). This full program takes the list of points as its argument and returns the centre and radius of the smallest enclosing circle. It generates circumcircles for all sets of three points, and diameters for all sets of two points, checks which include all of the points and then picks the one with the smallest radius.
Code for the make_circumcircle bit was inspired by code at this site, in turn inspired by Wikipedia.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f187313%2fgolf-the-smallest-circle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Wolfram Language (Mathematica), 28 bytes
BoundingRegion[#,"MinDisk"]&
Try it online!
Built-ins are handy here. Output is a disk object with the centre and radius. Like others, I’ve found the 2nd and 3rd test cases to be different to the question.
$endgroup$
1
$begingroup$
I was expecting a Mathematica built-in. But I wasn't expecting Mathematica to have "disk objects".
$endgroup$
– Robin Ryder
4 hours ago
add a comment |
$begingroup$
Wolfram Language (Mathematica), 28 bytes
BoundingRegion[#,"MinDisk"]&
Try it online!
Built-ins are handy here. Output is a disk object with the centre and radius. Like others, I’ve found the 2nd and 3rd test cases to be different to the question.
$endgroup$
1
$begingroup$
I was expecting a Mathematica built-in. But I wasn't expecting Mathematica to have "disk objects".
$endgroup$
– Robin Ryder
4 hours ago
add a comment |
$begingroup$
Wolfram Language (Mathematica), 28 bytes
BoundingRegion[#,"MinDisk"]&
Try it online!
Built-ins are handy here. Output is a disk object with the centre and radius. Like others, I’ve found the 2nd and 3rd test cases to be different to the question.
$endgroup$
Wolfram Language (Mathematica), 28 bytes
BoundingRegion[#,"MinDisk"]&
Try it online!
Built-ins are handy here. Output is a disk object with the centre and radius. Like others, I’ve found the 2nd and 3rd test cases to be different to the question.
answered 4 hours ago
Nick KennedyNick Kennedy
3,5646 silver badges10 bronze badges
3,5646 silver badges10 bronze badges
1
$begingroup$
I was expecting a Mathematica built-in. But I wasn't expecting Mathematica to have "disk objects".
$endgroup$
– Robin Ryder
4 hours ago
add a comment |
1
$begingroup$
I was expecting a Mathematica built-in. But I wasn't expecting Mathematica to have "disk objects".
$endgroup$
– Robin Ryder
4 hours ago
1
1
$begingroup$
I was expecting a Mathematica built-in. But I wasn't expecting Mathematica to have "disk objects".
$endgroup$
– Robin Ryder
4 hours ago
$begingroup$
I was expecting a Mathematica built-in. But I wasn't expecting Mathematica to have "disk objects".
$endgroup$
– Robin Ryder
4 hours ago
add a comment |
$begingroup$
R, 67 bytes
function(x,a=nlm(function(y)max(Mod(x-y%*%c(1,1i))),0:1))c(a$e,a$m)
Try it online!
Takes input as a vector of complex coordinates. Mod
is the distance (modulus) in the complex plane and nlm
is an optimization function: it finds the position of the center (a$e
) which minimizes the maximum distance to the input points, and gives the corresponding distance (a$m
), i.e. the radius. Accurate to 3-6 digits; the TIO footer rounds the output to 2 digits.
nlm
takes a numeric vector as input: the y%*%c(1,1i)
business converts it to a complex.
$endgroup$
add a comment |
$begingroup$
R, 67 bytes
function(x,a=nlm(function(y)max(Mod(x-y%*%c(1,1i))),0:1))c(a$e,a$m)
Try it online!
Takes input as a vector of complex coordinates. Mod
is the distance (modulus) in the complex plane and nlm
is an optimization function: it finds the position of the center (a$e
) which minimizes the maximum distance to the input points, and gives the corresponding distance (a$m
), i.e. the radius. Accurate to 3-6 digits; the TIO footer rounds the output to 2 digits.
nlm
takes a numeric vector as input: the y%*%c(1,1i)
business converts it to a complex.
$endgroup$
add a comment |
$begingroup$
R, 67 bytes
function(x,a=nlm(function(y)max(Mod(x-y%*%c(1,1i))),0:1))c(a$e,a$m)
Try it online!
Takes input as a vector of complex coordinates. Mod
is the distance (modulus) in the complex plane and nlm
is an optimization function: it finds the position of the center (a$e
) which minimizes the maximum distance to the input points, and gives the corresponding distance (a$m
), i.e. the radius. Accurate to 3-6 digits; the TIO footer rounds the output to 2 digits.
nlm
takes a numeric vector as input: the y%*%c(1,1i)
business converts it to a complex.
$endgroup$
R, 67 bytes
function(x,a=nlm(function(y)max(Mod(x-y%*%c(1,1i))),0:1))c(a$e,a$m)
Try it online!
Takes input as a vector of complex coordinates. Mod
is the distance (modulus) in the complex plane and nlm
is an optimization function: it finds the position of the center (a$e
) which minimizes the maximum distance to the input points, and gives the corresponding distance (a$m
), i.e. the radius. Accurate to 3-6 digits; the TIO footer rounds the output to 2 digits.
nlm
takes a numeric vector as input: the y%*%c(1,1i)
business converts it to a complex.
edited 4 hours ago
answered 4 hours ago
Robin RyderRobin Ryder
1,9182 silver badges18 bronze badges
1,9182 silver badges18 bronze badges
add a comment |
add a comment |
$begingroup$
JavaScript (ES6), 298 bytes
Returns an array [x, y, r]
.
p=>p.map(m=h=>p.map(i=>[0,...p].map(j=>!p.every(([x,y])=>H(x-X,y-Y)<=r,([a,A]=h,[b,B]=i),X=j?x=([c,C]=j,P=a*a+A*A,Q=b*b+B*B,R=c*c+C*C,d=c*(l=A-B)+a*(B-=C)+b*(C-=A),P*B+Q*C+R*l)/d/2:(a/=2)+(x=b/2),Y=j?y=(P*(c-b)+Q*(a-c)+R*(b-a))/d/2:(A/=2)+(y=B/2),H=Math.hypot,r=H(x-a,y-A))|r>m||(o=[X,Y,m=r]))))&&o
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 298 bytes
Returns an array [x, y, r]
.
p=>p.map(m=h=>p.map(i=>[0,...p].map(j=>!p.every(([x,y])=>H(x-X,y-Y)<=r,([a,A]=h,[b,B]=i),X=j?x=([c,C]=j,P=a*a+A*A,Q=b*b+B*B,R=c*c+C*C,d=c*(l=A-B)+a*(B-=C)+b*(C-=A),P*B+Q*C+R*l)/d/2:(a/=2)+(x=b/2),Y=j?y=(P*(c-b)+Q*(a-c)+R*(b-a))/d/2:(A/=2)+(y=B/2),H=Math.hypot,r=H(x-a,y-A))|r>m||(o=[X,Y,m=r]))))&&o
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 298 bytes
Returns an array [x, y, r]
.
p=>p.map(m=h=>p.map(i=>[0,...p].map(j=>!p.every(([x,y])=>H(x-X,y-Y)<=r,([a,A]=h,[b,B]=i),X=j?x=([c,C]=j,P=a*a+A*A,Q=b*b+B*B,R=c*c+C*C,d=c*(l=A-B)+a*(B-=C)+b*(C-=A),P*B+Q*C+R*l)/d/2:(a/=2)+(x=b/2),Y=j?y=(P*(c-b)+Q*(a-c)+R*(b-a))/d/2:(A/=2)+(y=B/2),H=Math.hypot,r=H(x-a,y-A))|r>m||(o=[X,Y,m=r]))))&&o
Try it online!
$endgroup$
JavaScript (ES6), 298 bytes
Returns an array [x, y, r]
.
p=>p.map(m=h=>p.map(i=>[0,...p].map(j=>!p.every(([x,y])=>H(x-X,y-Y)<=r,([a,A]=h,[b,B]=i),X=j?x=([c,C]=j,P=a*a+A*A,Q=b*b+B*B,R=c*c+C*C,d=c*(l=A-B)+a*(B-=C)+b*(C-=A),P*B+Q*C+R*l)/d/2:(a/=2)+(x=b/2),Y=j?y=(P*(c-b)+Q*(a-c)+R*(b-a))/d/2:(A/=2)+(y=B/2),H=Math.hypot,r=H(x-a,y-A))|r>m||(o=[X,Y,m=r]))))&&o
Try it online!
answered 5 hours ago
ArnauldArnauld
86.6k7 gold badges102 silver badges353 bronze badges
86.6k7 gold badges102 silver badges353 bronze badges
add a comment |
add a comment |
$begingroup$
Jelly, 100 bytes
ZÆm,Hñ/$
_²§½
1ịṭƊZIṚṙ€1N1¦,@ṭ@²§$µḢZḢ×Ø1œị$SḤ÷@P§
ZṀ+ṂHƲ€_@Ç+ḷʋ⁸,_²§½ʋḢ¥¥
œc3Ç€;ŒcÑ€$2ŀ_ƭƒⱮṀ¥Ðḟ⁸ṚÞḢ
Try it online!
In contrast to my Wolfram language answer, Jelly needs quite a lot of code to achieve this (unless I’m missing something!). This full program takes the list of points as its argument and returns the centre and radius of the smallest enclosing circle. It generates circumcircles for all sets of three points, and diameters for all sets of two points, checks which include all of the points and then picks the one with the smallest radius.
Code for the make_circumcircle bit was inspired by code at this site, in turn inspired by Wikipedia.
$endgroup$
add a comment |
$begingroup$
Jelly, 100 bytes
ZÆm,Hñ/$
_²§½
1ịṭƊZIṚṙ€1N1¦,@ṭ@²§$µḢZḢ×Ø1œị$SḤ÷@P§
ZṀ+ṂHƲ€_@Ç+ḷʋ⁸,_²§½ʋḢ¥¥
œc3Ç€;ŒcÑ€$2ŀ_ƭƒⱮṀ¥Ðḟ⁸ṚÞḢ
Try it online!
In contrast to my Wolfram language answer, Jelly needs quite a lot of code to achieve this (unless I’m missing something!). This full program takes the list of points as its argument and returns the centre and radius of the smallest enclosing circle. It generates circumcircles for all sets of three points, and diameters for all sets of two points, checks which include all of the points and then picks the one with the smallest radius.
Code for the make_circumcircle bit was inspired by code at this site, in turn inspired by Wikipedia.
$endgroup$
add a comment |
$begingroup$
Jelly, 100 bytes
ZÆm,Hñ/$
_²§½
1ịṭƊZIṚṙ€1N1¦,@ṭ@²§$µḢZḢ×Ø1œị$SḤ÷@P§
ZṀ+ṂHƲ€_@Ç+ḷʋ⁸,_²§½ʋḢ¥¥
œc3Ç€;ŒcÑ€$2ŀ_ƭƒⱮṀ¥Ðḟ⁸ṚÞḢ
Try it online!
In contrast to my Wolfram language answer, Jelly needs quite a lot of code to achieve this (unless I’m missing something!). This full program takes the list of points as its argument and returns the centre and radius of the smallest enclosing circle. It generates circumcircles for all sets of three points, and diameters for all sets of two points, checks which include all of the points and then picks the one with the smallest radius.
Code for the make_circumcircle bit was inspired by code at this site, in turn inspired by Wikipedia.
$endgroup$
Jelly, 100 bytes
ZÆm,Hñ/$
_²§½
1ịṭƊZIṚṙ€1N1¦,@ṭ@²§$µḢZḢ×Ø1œị$SḤ÷@P§
ZṀ+ṂHƲ€_@Ç+ḷʋ⁸,_²§½ʋḢ¥¥
œc3Ç€;ŒcÑ€$2ŀ_ƭƒⱮṀ¥Ðḟ⁸ṚÞḢ
Try it online!
In contrast to my Wolfram language answer, Jelly needs quite a lot of code to achieve this (unless I’m missing something!). This full program takes the list of points as its argument and returns the centre and radius of the smallest enclosing circle. It generates circumcircles for all sets of three points, and diameters for all sets of two points, checks which include all of the points and then picks the one with the smallest radius.
Code for the make_circumcircle bit was inspired by code at this site, in turn inspired by Wikipedia.
edited 4 hours ago
answered 4 hours ago
Nick KennedyNick Kennedy
3,5646 silver badges10 bronze badges
3,5646 silver badges10 bronze badges
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f187313%2fgolf-the-smallest-circle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Link to sandbox post
$endgroup$
– Barranka
8 hours ago
1
$begingroup$
Is anyone else getting the same radius but with different coordinates for the 2nd and 3rd test cases?
$endgroup$
– Arnauld
6 hours ago
1
$begingroup$
“if there are three (non co-linear) points, it's possible to get the equation of a circle that touches them all”—but that may not be the smallest enclosing circle. For the three vertices of an obtuse triangle, the smallest enclosing circle is the one whose diameter is the longest side.
$endgroup$
– Anders Kaseorg
5 hours ago
1
$begingroup$
@Arnauld Same as you. For test case 2, I get center (-1.73, 0.58) and for test case 3 (5.5, -4).
$endgroup$
– Robin Ryder
4 hours ago
$begingroup$
@Arnauld thanks for your comment. I have edited the post accordingly
$endgroup$
– Barranka
2 hours ago