Skip to content

Instantly share code, notes, and snippets.

@itsabdelrahman
Created July 27, 2024 09:52
Show Gist options
  • Save itsabdelrahman/4fe9ceaac485cb155f61eaa51edf79f8 to your computer and use it in GitHub Desktop.
Save itsabdelrahman/4fe9ceaac485cb155f61eaa51edf79f8 to your computer and use it in GitHub Desktop.
const { lvar, add, sub, mul, and, between, run } = require("logicjs");
const circle = ({ a, b, r, x, y }) => {
const xMinusA = lvar();
const yMinusB = lvar();
const xMinusASquared = lvar();
const yMinusBSquared = lvar();
const rSquared = lvar();
return [
sub(x, a, xMinusA), // x−a
sub(y, b, yMinusB), // y−b
mul(xMinusA, xMinusA, xMinusASquared), // (x−a)^2
mul(yMinusB, yMinusB, yMinusBSquared), // (y−b)^2
mul(r, r, rSquared), // r^2
add(xMinusASquared, yMinusBSquared, rSquared), // (x−a)^2 + (y−b)^2 = r^2
];
};
const x = lvar();
const y = lvar();
const constraints = [
between(-10, 10, x),
between(-10, 10, y),
// https://planetcalc.com/8098/?x1=0&y1=0&r1=5&x2=10&y2=0&r2=5
...circle({ a: 0, b: 0, r: 5, x, y }),
...circle({ a: 10, b: 0, r: 5, x, y }),
];
const result = run(and(...constraints), [x, y]);
console.log(result); // [ [ 5, 0 ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment