Largest circle inside a polygon: a linear programming example, solved and checked
The largest circle inside a polygon is a classic linear programming problem. Here it is solved by a real solver (HIGHS) and checked: every constraint, the dual, and the optimality certificate. Change the numbers below and solve your own.
Solve your own version free, no account needed
The problem
Find the largest circle that fits inside the region 0 <= x <= 2, 0 <= y <= 2, x + y <= 3. Report its radius.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- (a, b) is the centre and r the radius. The circle stays inside a side when the centre is at least r from it: a − r ≥ 0 for the side x = 0 and a + r ≤ 2 for the side x = 2; the same for b and y.
- The slanted side x + y = 3: the centre's distance to it is (3 − a − b) ÷ √2, so the circle fits when a + b + √2 r ≤ 3.
The model
Each line has a plain-language note on the right.
| make this as large as possible | |
| limit 1: left side must reach at least 0 | |
| limit 2: left side cannot exceed 2 | |
| limit 3: left side must reach at least 0 | |
| limit 4: left side cannot exceed 2 | |
| limit 5: left side cannot exceed 3 | |
| radius of circle cannot be negative |
In matrix form
With its numbers:
What is being decided
There are 3 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| a | real number | x-coordinate of circle center |
| r | real number | radius of circle |
| b | real number | y-coordinate of circle center |
Goal and limits
Maximize the objective subject to 6 constraints, of which 1 are simple bounds on a single variable.
Problem type
LP, complexity level 1: a small linear program. Solved with HIGHS at the Medium solver tier.
The answer at a glance
The optimal value of each decision, as the solver returned it.
Which constraints hold the answer in place
Binding (no slack left): (1) a - r >= 0; (3) b - r >= 0; (5) a + b + 1.414213562*r <= 3. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | a - r >= 0 | 0 | 0 | 0 | yes |
| 2 | a + r <= 2 | 1.75736 | 2 | 0.242641 | |
| 3 | b - r >= 0 | 0 | 0 | 0 | yes |
| 4 | b + r <= 2 | 1.75736 | 2 | 0.242641 | |
| 5 | a + b + 1.414213562*r <= 3 | 3 | 3 | 0 | yes |
| 6 | r >= 0 | 0.87868 | 0 | 0.87868 |
Algebraic model (low level)
Decision variables, objective and every constraint written out; this is exactly what the solver receives.
Standard form
As a linear program: minimize cᵀx subject to A x ≤ b (the inequality rows) and A_eq x = b_eq (the equations); a maximization is written as minimizing the negative.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
a = cp.Variable(name='a') # x-coordinate of circle center
r = cp.Variable(name='r') # radius of circle
b = cp.Variable(name='b') # y-coordinate of circle center
objective = cp.Maximize(r)
constraints = [
a - r >= 0,
a + r <= 2,
b - r >= 0,
b + r <= 2,
a + b + 1.4142135623730951*r <= 3,
r >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('a =', a.value)
print('r =', r.value)
print('b =', b.value)The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. Strong duality: primal and dual optima are equal (0.87868).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'a - r >= 0' (objective change per extra unit of its right-hand side) | -0.292893 |
| y2 | price of constraint 'a + r <= 2' (objective change per extra unit of its right-hand side) | 0 |
| y3 | price of constraint 'b - r >= 0' (objective change per extra unit of its right-hand side) | -0.292893 |
| y4 | price of constraint 'b + r <= 2' (objective change per extra unit of its right-hand side) | 0 |
| y5 | price of constraint 'a + b + 1.414213562*r <= 3' (objective change per extra unit of its right-hand side) | 0.292893 |
Solve your own
This is the model as the app reads it. Change any number and solve it in the free box on the front page (no account needed).
maximize r
subject to
a - r >= 0
a + r <= 2
b - r >= 0
b + r <= 2
a + b + 1.4142135623730951 * r <= 3
r >= 0
# a = x-coordinate of circle center
# b = y-coordinate of circle center
# r = radius of circle
# asked: radius r of the largest inscribed circleChecked: the app's answer matches the reference answer in our test set, which two solvers (HIGHS, CLARABEL) agree on. Built 2026-11-10.
Related examples
Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.