react-plot
Version:
Library of React components to render SVG 2D plots.
54 lines • 2.19 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const halfPi = Math.PI / 2;
const twoPi = Math.PI * 2;
export function Circle({ style, size }) {
return _jsx("circle", { r: size / 2, style: style });
}
export function Square({ style, size }) {
const x = size / 2;
return _jsx("rect", { x: -x, y: -x, width: size, height: size, style: style });
}
export function Diamond({ size, style }) {
const x = size / 2;
return _jsx("polygon", { points: `0,${-x} ${x},0 0,${x} ${-x},0`, style: style });
}
export function Triangle({ style, size }) {
const height = (Math.sqrt(3) * size) / 2;
const x = size / 2;
return (_jsx("polygon", { transform: `translate(0, -${(size - height) / 2})`, points: `${-x},${x} ${x},${x} 0,${x - height}`, style: style }));
}
export function Cross({ size, style }) {
const x = size / 2;
return (_jsxs("g", { strokeWidth: 1, style: style, children: [_jsx("line", { x1: 0, x2: 0, y1: x, y2: -x }), _jsx("line", { x1: -x, x2: x, y1: 0, y2: 0 })] }));
}
export function XMark(props) {
return (_jsx("g", { transform: "rotate(45)", children: _jsx(Cross, { ...props }) }));
}
export function Pentagon({ style, size }) {
return (_jsx("polygon", { points: Array.from(getPolyPoints(size, 5)).join(' '), style: style }));
}
export function Star({ style, size }) {
const ext = Array.from(getPolyPoints(size, 5, 0));
const int = Array.from(getPolyPoints(size / 2.5, 5, (2 * Math.PI) / 10));
const points = [];
for (let i = 0; i < ext.length; i++) {
points.push(ext[i], int[i]);
}
return _jsx("polygon", { points: points.join(' '), style: style });
}
export function Hexagon({ style, size }) {
return (_jsx("polygon", { points: Array.from(getPolyPoints(size, 6)).join(' '), style: style }));
}
/**
* https://math.stackexchange.com/a/3582355
*/
function* getPolyPoints(diameter, k, angle = 0) {
const r = diameter / 2;
for (let i = 0; i < k; i++) {
const calculation = (twoPi * i) / k - halfPi + angle;
const x = r * Math.cos(calculation);
const y = r * Math.sin(calculation);
yield `${x},${y}`;
}
}
//# sourceMappingURL=Markers.js.map