| Bytes | Lang | Time | Link |
|---|---|---|---|
| 052 | Wolfram Language Mathematica | 240711T192306Z | Sanguine |
| 117 | Typst with CeTZ package | 250308T125950Z | Wallbrea |
| 036 | MATL | 240712T143914Z | Luis Men |
| 143 | Javascript | 240713T005151Z | ccprog |
| 145 | Scratch 3.23.1 | 240713T115641Z | Glory2Uk |
| 060 | Logo | 240712T152602Z | Neil |
| 138 | Ruby | 240711T210954Z | Level Ri |
| 098 | R | 240712T092046Z | Dominic |
| 056 | APLDyalog Unicode | 240712T053759Z | akamayu |
| 064 | Python | 240711T190654Z | Albert.L |
Wolfram Language (Mathematica), 62 52 bytes
Thanks to Conor O'Brien and Joao-3 for the reduction!
Graphics@ResourceFunction["ReuleauxPolygon"][#1,#2]&
Of course Wolfram has a way to do this. First input is arc radius, and second is \$n\$ number of sides.
Example (old answer) with input 100, 5:
Typst with CeTZ package, 117 Bytes
#import"@preview/cetz:0.2.2":*
#let f(n)=canvas({let k=180deg/n;for i in range(n){draw.arc((),start:k*2*i,delta:k)}})
I used an older version of CeTZ to meet the rule that the implementations should be made before the challenge. You will need an older version of Typst to compile it.
MATL, 36 bytes
t:!=ZFt9W:8W/qt!J*+le-|wq|X>>a[]e0YG
Try at MATL Online!
For input n, this computes the n-th roots of unity, and colours each pixel if and only if it is at a distance D or less from all roots, where D is the maximum distance between pairs of roots.
The code uses this trick (adapted to MATL) to compute the roots of unity.
t % Implicitly take input: n. Duplicate
:! % Range, transpose. Gives the column vector [1; 2; ...; n]
= % Equal? element-wise. Gives [0; 0; ...; 1]
ZF % FFT. Gives roots of unity (*)
t % Duplicate
9W: % 2^9, range. Gives the row vector [0, 1, ..., 512]
8W/ % Divide each ech entry by 2^8
q % Subtract 1. Gives [-0.99609375, -0.9921875, ..., 1]
t!J* % Duplicate, transpose, multiply element-wise by imaginart unit
+ % Add element-wise with broadcast. Gives a 512×512 complex grid ranging
% from -0.99609375 to 1 on each axis
le % Linearize (in column-major order) to a row vector of length 512^2 (**)
-| % Absolute difference between (*) and (**), element-wise with broadcast.
% Gives an n×512^2 distance matrix
w % Swap. Moves original copy of (*) to the top
q|X> % Subtract 1, absolute value, maximum. Gives distance D
> % Greater than? element-wise. Gives an n×512^2 logical matrix
a % Any. Gives a row vector of length 512^2, containing 1 if the pixel
% is at a distance greater than D from at least one circle center, and
% 0 otherwise
[]e % Reshape as a square (512×512) matrix
0YG % Write as image file. Implicitly display
Javascript, 154 147 143 bytes
A function returning a string containing an SVG for inline insertion into a HTML page.
2 bytes less due to changed count.
5 byte saved thx to hints by @noodle person and @Neil.
4 bytes saved by using eval("with(Math)for..., thx to @noodle person.
f=
n=>`<svg viewBox=-4,-1,9,2><path d=M${eval("with(Math)for(i=s=0;i<n;i++)s+=[sin(a=2*i*PI/n),cos(a)]+'A'+[r=(2+2*cos(PI/n))**.5,r,0,0,0]")}0,1>`
<input type=number size=2 min=3 step=2 oninput=w.setHTMLUnsafe(f(value|0))><div id=w>
Ungolfed
f = n => {
s = '<svg viewBox=-4,-1,9,2><path d=M'
for(i=s=0;i<n;i++) {
a = 2 * i * Math.PI / n
p = [Math.sin(a), Math.cos(a)]
r = (2 + 2 * Math.cos(Math.PI / n)) ** 0.5
s += p + 'A' + [r,r,0,0,0]
}
return s += '0,1>'
}
Scratch 3.23.1, 160 145 bytes
- thanks to noodle person for syntax correction & golfing
when gf clicked
erase all
pen down
ask()and wait
repeat(answer
turn cw((180)/(answer))degrees
repeat((180)/(answer
move(3)steps
turn cw(1)degrees
A program that inputs \$n\$ - a number of sides and plots a Reuleux polygon of \$n\$ sides. Radius length is fixed.
Logo, 61 60 bytes
to r:n
repeat:n[pd arc 90 180/:n pu fd 90 rt 180-180/:n]
end
Unfortunately the online logo resource I used to use is no longer available, but you can paste the code into the textbox on https://rmmh.github.io/papert/static/ and run it. (Also paste the following code to actually get anything to run.)
clear
r 5
(Substitute your desired value for 5.)
It's also possible to draw a padded polygon; this is still a curve of constant width but that with w is now d+2p where p is the extra padding:
to r:n:d:p
repeat:n[pd arc:p 180/:n lt 180 arc:d+:p 180/:n pu fd:d lt 180/:n]
end
clear
r 5 100 25
Ruby, 152 142 138 bytes
->n{s='<svg viewBox="-50-9 99 18"><path d="M9 0'
n.times{|j|s<<"A#{r=9/(k=1i**(1.0/n)).imag} #{r} 0 0,0 %f %f"%(9*k**(4*~j)).rect}
s+'">'}
A function returning a string containing an SVG image such as the one below. The n points always fall on a circle of radius 9. The radius of curvature of the sides varies according to the value of n.
<svg viewBox="-50-9 99 18"><path d="M9 0A18.000000000000004 18.000000000000004 0 0,0 -4.500000 -7.794229A18.000000000000004 18.000000000000004 0 0,0 -4.500000 7.794229A18.000000000000004 18.000000000000004 0 0,0 9.000000 0.000000">
Viewing notes
The SVG has been tested in Chrome and Edge. A concern has been raised about the lack of space between the two -50s defining coordinates of the corner of the viewbox, but that makes no difference to the output in either of these browsers.
There is an issue with the browsers sizing the viewbox based on the width only and ignoring the height, requiring the user to scroll vertically. It is recommended to hit Run code snippet before expanding, to avoid getting a horizontally wide but vertically very short window requiring excessive scrolling. Narrowing the browser window helps with this.
I have reduced the radius of the output from 40 units to 9 units to make it easier to get the whole Reuleaux polygon into view (this also saves 3 bytes.) I have also reduced the height of the viewbox to help eliminate vertical scrolling (this saves another byte.)
R, 116 111 98 bytes
Edit: no longer requiring radius to be given as an input saves 13 bytes.
\(n,i=1:396/99-2,v=1i^(1:n*4/n),R=abs(1-v[n/2]),`~`=sapply)image(i~\(x)i~\(y)all(abs(x+y*1i-v)<R))
Try it at rdrr.io.
(note that rdrr.io runs on an earlier version of R that does not use the \ syntax for a function definition, so the code in the link is 21 bytes longer)
APL(Dyalog Unicode), 58 56 bytes SBCS
-2 bytes due to unused variable assignment B←.
A dfn that takes the number of edges and outputs a bitmap of the shape.
{(C-C⊖⍨⌈⍵÷2)∧.>⍥|C∘.-I∘.+0J1×I←40÷⍨¯41+⍳81⊣C←¯1*⍵÷⍨+⍨⍳⍵}
It uses complex numbers to encode points on the 2D plane.
{(C-C⊖⍨⌈⍵÷2)∧.>⍥|C∘.-I∘.+0J1×I←40÷⍨¯41+⍳81⊣C←¯1*⍵÷⍨+⍨⍳⍵}
C←¯1*⍵÷⍨+⍨⍳⍵ ⍝ vertces
I∘.+0J1×I←40÷⍨¯41+⍳81 ⍝ points on the plane
C∘.- ⍝ displacement vectors to the vertices
(C-C⊖⍨⌈⍵÷2) ⍝ displacement vectors between oppisite vertices
∧.>⍥| ⍝ are the points within the reuleaux polygon
Python, 64 bytes
from turtle import*
def f(n,d):
for c in[0,d]*n:circle(c,180/n)
ATO Does not work for this (missing _tkinter)!
Nor does TIO.
History: 80,82,64.
How?
Observing that the arc lengths always sum to half a full circle we let the turtle draw a circle omitting every other of a total of \$2n\$ equal length arcs


