distance to the edge of a rounded rectangle I'm trying to figure out a formula to create a rounded rectangle... but I need to do it by finding the distance from $(0, 0)$ to the line/edge of the rectangle given the angle provided as a vector.

Ok.. so here is the formula to plot the rectangle:  $\left(\frac{x}{a}\right)^n + \left(\frac{y}{b}\right)^n = 1$

In my usage a and b will both be 1. Thus the formula can be simplified into $x^n + y^n = 1$

http://www.wolframalpha.com/input/?i=x%5E4+%2B+y%5E4+%3D+1



My input will be a point in space that can represent a vector.  Say it's (.5,.5).  The angle of this vector is $45^\circ$ and therefore it intersects with the plotted line at a specific distance from $(0, 0)$.  I don't need to know the $x$, $y$ of the plot per se.  I need to know the distance to that point.  If I have the x,y I can easily calculate the distance.  But maybe there is a way to calculate the distance directly.

I know I have: $\left(\frac{x}{a}\right)^n + \left(\frac{y}{b}\right)^n = 1$

and I have $d = \sqrt{x^2 + y^2}$

But alone that isn't enough info for me to figure it out.  I can see a pattern in there though.  OH crap they're the same formula, except that $n=2$, where I want $n=4$ (or $n&gt;4$).  When $d = 1$ the square root of $1 = 1$.

I was able to solve for d like this - but I don't know if this is helping...


  1 = x^n + y^n
  
  0 = x^n + y^n - 1
  
  d = sqr(x^2 + y^2)
  
  0 = sqr(x^2 + y^2) - d
  
  d = 1 + sqr(x^2 + y^2) - x^n - y^n


But still.. I don't know x and y on the plot.  I'm given x,y in space and have to find the distance to the plotted line where it intersects with that vector.

Maybe rather I need some sort of intersection type formula??  Any ideas?

EDIT ----

Really what I need is to figure out the distance to the line at any given angle.

EDIT 2 --

What about this approach..

Here are the knowns.. I have an x,y out in space.  Therefore I have an angle and a distance for that vector.  I also know the base size of the rounded rectangle.

If it was a circle it's easy cause the radius of a circle is constant.



My answer here might not be very mathematical in terms of equation syntax.. but I'm a programmer, not a mathematician.. 

It should be possible to calculate the difference between the radius of the circle (known if the object were a circle) and the "radius" at any point on the rounded rectangle.

At an angle of $\theta$ they are the same.  At and angle of 90 they are the same.  But at points between the two they are not.

If the radius of the circle is 1.. then you have $d = 1 + \sqrt{x^2 + y^2} - x^n - y^n$ for the "radius" or distance to the edge of the rounded rectangle.

The difference between the two is delta $d = 1 - x^n - y^n$

So if our desired rectangle is S in size instead of 1 - S is a scale factor over the 1.

If I normalize the vector $(x, y)$ and make it's length 1.  Then use that x, y to figure out the delta d. I can then scale that dd by S and add it to d to get the distance at that angle.

Checking to see if it works in code.
Let the vector be $(a,b)$.  The line it is on is $y=\frac ba x$ (assuming it is not vertical.)  Plug this into your superellipse and get $x^n+\left(\frac ba x\right)^n=1$ or $x^n= \frac 1{1+(\frac ba)^n}=\frac {a^n}{a^n+b^n}, x=\left(\frac {a^n}{a^n+b^n}\right)^{\frac 1n}$ for the intersection point.
For what its worth here is a solution using Qt that finds the intersecting point between a line starting at the center of a rounding rectangle to some place outside of the shape. You have to specify the Rectangle, Radius of the rounded edges and a point B which is outside of the rounded rectangle.

#include &lt;QApplication&gt;
#include &lt;QDebug&gt;
#include &lt;QGraphicsItem&gt;
#include &lt;QGraphicsScene&gt;
#include &lt;QGraphicsView&gt;
#include &lt;QLine&gt;
#include &lt;QPainterPath&gt;
#include &lt;QPoint&gt;
#include &lt;QRect&gt;
#include &lt;limits&gt;
#include &lt;math.h&gt;

#define kDegToRad 0.0174532925

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool circleLineIntersectPoint( double aRadius, QPointF aA, QPointF aB, QPointF aC, QPointF &amp;aIntersection)
{
  bool tReturn = true; // false = not intersection point, true == insersected
  aIntersection = QPointF();
  QPointF tD;
  tD.setX(aB.x() - aA.x());
  tD.setY(aB.y() - aA.y());
  if ((tD.x() == 0) &amp;&amp; (tD.y() == 0))
  {
    // A and B are the same points, no way to calculate intersection
    tReturn = false;
    return tReturn;
  }

  double tDL = (tD.x() * tD.x() + tD.y() * tD.y());
  double tT = ((aC.x() - aA.x()) * tD.x() + (aC.y() - aA.y()) * tD.y()) / tDL;

  // point on a line nearest to circle center
  QPointF tNearest;
  tNearest.setX(aA.x() + tT * tD.x());
  tNearest.setY(aA.y() + tT * tD.y());

  QLineF tLine(tNearest.x(), tNearest.y(), aC.x(), aC.y());
  double tDist = tLine.length();

  if (tDist == aRadius)
  {
    // line segment touches circle; one intersection point
    aIntersection = tNearest;

    if (tT &lt; 0 || tT &gt; 1)
    {
      // intersection point is not actually within line segment
      tReturn = false;
    }
  }
  else if (tDist &lt; aRadius)
  {
    // two possible intersection points

    double tDT = sqrt(aRadius * aRadius - tDist * tDist) / sqrt(tDL);

    // intersection point nearest to A
//    double tT1 = tT - tDT;
//    QPointF tI1;
//    tI1.setX(aA.x() + tT1 * tD.x());
//    tI1.setY(aA.y() + tT1 * tD.y());
//    if (tT1 &lt; 0 || tT1 &gt; 1)
//    {
//      // intersection point is not actually within line segment
//      tReturn = false;
//    }

//    if(tReturn)  // intersection point farthest from A
//    {
      double tT2 = tT + tDT;
      QPointF tI2;
      tI2.setX(aA.x() + tT2 * tD.x());
      tI2.setY(aA.y() + tT2 * tD.y());
      if (tT2 &lt; 0 || tT2 &gt; 1)
      {
        // intersection point is not actually within line segment
        tReturn = false;
      }
      else
        aIntersection = tI2;
//    }
  }
  else
  {
    // no intersection
    tReturn = false;
  }

  return tReturn;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int rectangleLineIntersectPoint(QRectF aRect, QPointF aB, QPointF &amp;aPoint)
{
  // Define a line from the center of the rectangle to point B
  QLineF tLine(aRect.center(), aB);

  QPointF tReturn;
  int tType = 0;

  // See if the line intersects the top line of the rectangle
  QLineF tTopLine(aRect.topLeft(), aRect.topRight());
  if(QLineF::BoundedIntersection == tTopLine.intersect(tLine, &amp;aPoint)) tType = 1; // Top Line
  else
  {
    // See if the line intersects the right line of the rectangle
    QLineF tRightLine (aRect.topRight(), aRect.bottomRight());
    if(QLineF::BoundedIntersection == tRightLine.intersect(tLine, &amp;aPoint)) tType = 2; // Right Line
    else
    {
      // See if the line intersects the bottom line of the rectangle
      QLineF tBottomLine(aRect.bottomLeft(), aRect.bottomRight());
      if(QLineF::BoundedIntersection == tBottomLine.intersect(tLine, &amp;aPoint)) tType = 3; // Bottom Line
      else
      {
        // See if the line intersects the left line of the rectangle
        QLineF tLeftLine(aRect.topLeft(), aRect.bottomLeft());
        if(QLineF::BoundedIntersection == tLeftLine.intersect(tLine, &amp;aPoint)) tType = 4; // Left Line
        else
          tType = 0; // This should never happen since the center of the line is the center of the rect
      }
    }
  }
  return tType;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool roundedRectLineIntersectionPoint(double aRadius, QRectF aRect, QPointF aB, QPointF &amp;aPoint)
{
  bool tReturn = true; // False = not intersection point

  // Compute the rectangle to line intersection point
  int tType = rectangleLineIntersectPoint(aRect, aB, aPoint);

  //  Based on the type figure out if we need to compute the intersection with 
  //  rounded part of the rounded rectangle
  QPointF tC; // Center of the circle that makes up the rounded part
  bool tInRoundedEdge = true;
  switch(tType)
  {
    case 0: tReturn = false; tInRoundedEdge = false; break;

    // Top Line
    case 1: if(aPoint.x() &lt;= aRect.left() + aRadius) // Top Left
              tC = QPointF(aRect.left() + aRadius, aRect.top() + aRadius);
            else
            if(aPoint.x() &gt;= aRect.right() - aRadius) // Top Right
              tC = QPointF(aRect.right() - aRadius, aRect.top() + aRadius);
            else
              tInRoundedEdge = false;
            break;

    // Right Line
    case 2: if(aPoint.y() &lt;= aRect.top() + aRadius) // Top right
              tC = QPointF(aRect.right() - aRadius, aRect.top() + aRadius);
            else
            if(aPoint.y() &gt;= aRect.bottom() - aRadius) // Bottom Right
              tC = QPointF(aRect.right() - aRadius, aRect.bottom() - aRadius);
            else
              tInRoundedEdge = false;
            break;
    // Bottom Line
    case 3: if(aPoint.x() &lt;= aRect.left() + aRadius) // Bottom Left
              tC = QPointF(aRect.left() + aRadius, aRect.bottom() - aRadius);
            else
            if(aPoint.x() &gt;= aRect.right() - aRadius) // Bottom Right
              tC = QPointF(aRect.right() - aRadius, aRect.bottom() - aRadius);
            else
              tInRoundedEdge = false;
            break;
    // Left Line
    case 4: if(aPoint.y() &lt;= aRect.top() + aRadius) // Top Left
              tC = QPointF(aRect.left() + aRadius, aRect.top() + aRadius);
            else
            if(aPoint.y() &gt;= aRect.bottom() - aRadius) // Bottom left
              tC = QPointF(aRect.left() + aRadius, aRect.bottom() - aRadius);
            else
              tInRoundedEdge = false;
            break;
  }

  if(tReturn &amp;&amp; tInRoundedEdge)
    tReturn = circleLineIntersectPoint( aRadius, aRect.center(), aB, tC, aPoint);

  return tReturn;
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  QGraphicsView view;
  QGraphicsScene *scene = new QGraphicsScene;
  view.setScene(scene);

  QRectF tRect(-300, -150, 600, 300);
  double tLineLength = 800.0;
  qreal tRadius = 100.0;

  QPainterPath tPath;
//  tPath.addRoundedRect(tRect, tRadius, Qt::AbsoluteSize);
  tPath.addRect(tRect);
  scene-&gt;addPath(tPath, QPen(Qt::red), QBrush(Qt::green));

  QPointF tPoint;
  for(double tIndex = 0; tIndex &lt; 360; tIndex+=1)
  {
    double tX = tLineLength * sin(tIndex * kDegToRad);
    double tY = tLineLength * cos(tIndex * kDegToRad);
    roundedRectLineIntersectionPoint(tRadius, tRect, QPointF(tX, tY), tPoint);
    scene-&gt;addLine(QLineF(tRect.center(), QPointF(tPoint.x(), tPoint.y())));
  }

  view.show();

  return app.exec();
}