Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 703745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:51:54+00:00 2026-05-14T03:51:54+00:00

Given a rectangle (w, h) and a pie slice with a radius less or

  • 0

Given a rectangle (w, h) and a pie slice with a radius less or equal to the smaller of both sides (w, h), a start angle and an end angle, how can I place the slice optimally in the rectangle so that it fills the room best (from an optical point of view, not mathematically speaking)?

I’m currently placing the pie slice’s center in the center of the rectangle and use the half of the smaller of both rectangle sides as the radius. This leaves plenty of room for certain configurations.

Examples to make clear what I’m after, based on the precondition that the slice is drawn like a unit circle (i.e. 0 degrees on positive X axis, then running clock-wise):

  • A start angle of 0 and an end angle of PI would lead to a filled lower half of the rectangle and an empty upper half. A good solution here would be to move the center up by 1/4*h.
  • A start angle of 0 and an end angle of PI/2 would lead to a filled bottom right quarter of the rectangle. A good solution here would be to move the center point to the top left of the rectangle and to set the radius to the smaller of both rectangle sides.

This is fairly easy for the cases I’ve sketched but it becomes complicated when the start and end angles are arbitrary. I am searching for an algorithm which determines center of the slice and radius in a way that fills the rectangle best. Pseudo code would be great since I’m not a big mathematician.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:51:55+00:00Added an answer on May 14, 2026 at 3:51 am

    The extrema of the bounding box of your arc are in the following format:

    x + x0 * r = 0
    x + x1 * r = w
    y + y0 * r = 0
    y + y1 * r = h
    

    The values x0, x1, y0 and y1 are found by taking the minimum and maximum values of up to 7 points: any tangential points that are spanned (i.e. 0, 90, 180 and 270 degrees) and the end points of the two line segments.

    Given the extrema of the axis-aligned bounding box of the arc (x0, y0), (x1, y1) the radius and center point can be calculated as follows:

    r = min(w/(x1-x0), h/(y1-y0)
    x = -x0 * r
    y = -y0 * r
    

    Here is an implementation written in Lua:

    -- ensures the angle is in the range [0, 360)
    function wrap(angle)
        local x = math.fmod(angle, 2 * math.pi)
        if x < 0 then
            x = x + 2 * math.pi
        end
        return x
    end
    
    function place_arc(t0, t1, w, h)
        -- find the x-axis extrema
        local x0 = 1
        local x1 = -1
        local xlist = {}
        table.insert(xlist, 0)
        table.insert(xlist, math.cos(t0))
        table.insert(xlist, math.cos(t1))
        if wrap(t0) > wrap(t1) then
            table.insert(xlist, 1)
        end
        if wrap(t0-math.pi) > wrap(t1-math.pi) then
            table.insert(xlist, -1)
        end
        for _, x in ipairs(xlist) do
            if x < x0 then x0 = x end
            if x > x1 then x1 = x end
        end
    
        -- find the y-axis extrema
        local ylist = {}
        local y0 = 1
        local y1 = -1
        table.insert(ylist, 0)
        table.insert(ylist, math.sin(t0))
        table.insert(ylist, math.sin(t1))
        if wrap(t0-0.5*math.pi) > wrap(t1-0.5*math.pi) then
            table.insert(ylist, 1)
        end
        if wrap(t0-1.5*math.pi) > wrap(t1-1.5*math.pi) then
            table.insert(ylist, -1)
        end
        for _, y in ipairs(ylist) do
            if y < y0 then y0 = y end
            if y > y1 then y1 = y end
        end
    
        -- calculate the maximum radius the fits in the bounding box
        local r = math.min(w / (x1 - x0), h / (y1 - y0))
    
        -- find x & y from the radius and minimum extrema
        local x = -x0 * r
        local y = -y0 * r
    
        -- calculate the final axis-aligned bounding-box (AABB)
        local aabb = {
            x0 = x + x0 * r,
            y0 = y + y0 * r,
            x1 = x + x1 * r,
            y1 = y + y1 * r
        }
    
        return x, y, r, aabb
    end
    
    function center_arc(x, y, aabb, w, h)
        dx = (w - aabb.x1) / 2
        dy = (h - aabb.y1) / 2
        return x + dx, y + dy
    end
    
    t0 = math.rad(60)
    t1 = math.rad(300)
    w = 320
    h = 240
    x, y, r, aabb = place_arc(t0, t1, w, h)
    x, y = center_arc(x, y, aabb, w, h)
    print(x, y, r)
    

    Example output:

    alt text

    alt text

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 364k
  • Answers 364k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would probably personally stick to MySQL, but if you… May 14, 2026 at 3:42 pm
  • Editorial Team
    Editorial Team added an answer The Page's Load event fires up sooner than the Button's… May 14, 2026 at 3:42 pm
  • Editorial Team
    Editorial Team added an answer well.. i didn't figure it out.. but did somethig different… May 14, 2026 at 3:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.