Need help with fixing my rounded rectangle calculation
Hi! I'm making script for Unity. I'm bad at understanding floating points calculations and geometry, so it might be silly. Help me please
I'm making rounded rectangle. Either pieces are missing or lerp seems ignoring corners. Using unit circle x^2+y^2=r^2. Might be sqrt() or comparison conditions
c#
public RoundRect(float width, float height, float r) {
if (width < r || height < r) { throw new Exception("Radius"); }
this.width = width - 2*r;
this.height = height - 2*r;
this.r = r;
public float CornerByAxisPoint(float p, bool positive) {
if (Mathf.Abs(p) > r) {
p = Mathf.Clamp(p, -r, r);
}
return Mathf.Sqrt(r * r - p * p) * (positive ? 1 : -1);
}
public Vector2 GetPointOnBase(float x, bool topSide) {
if (x <= width) { return new Vector2(x+(topSide ? 0 : -width), (topSide ? r : -r)); }
else if (x > width) { return new Vector2(x+(topSide ? 0 : -width*2-r), CornerByAxisPoint(x-width+(topSide ? 0 : -r), topSide)); }
else throw pointOut;
}
public Vector2 GetPointOnLegs(float y, bool rightSide) {
if (y <= height) { return new Vector2(0, y * (rightSide ? -1 : 1)); }
else if (y > height) { return new Vector2(CornerByAxisPoint(y-height, rightSide)+(rightSide ? -r : r), (rightSide ? -y : y)); }
else throw pointOut;
}
public Vector2 GetPoint(float p) {
if (p <= radWidth) {
return GetPointOnBase(p, true)+Vector2.right*r; }
else if (p > radWidth && p <= radWidth + radHeight) {
return GetPointOnLegs(p - radWidth, true)+Vector2.right*fullWidth; }
else if (p > radWidth + radHeight && p <= radWidth*2 + radHeight) {
return GetPointOnBase(p - (radWidth + radHeight), false)+Vector2.right*radWidth+Vector2.down*height;
} else if (p > radWidth*2 + radHeight && p <= radWidth*2 + radHeight*2) {
return GetPointOnLegs(p - (radWidth*2 + radHeight), false)+Vector2.down*height;
} else throw pointOut;
}
public Vector2 Interpolate(float t) => GetPoint(Mathf.Lerp(0f, perimeter, t));c#
public RoundRect(float width, float height, float r) {
if (width < r || height < r) { throw new Exception("Radius"); }
this.width = width - 2*r;
this.height = height - 2*r;
this.r = r;
public float CornerByAxisPoint(float p, bool positive) {
if (Mathf.Abs(p) > r) {
p = Mathf.Clamp(p, -r, r);
}
return Mathf.Sqrt(r * r - p * p) * (positive ? 1 : -1);
}
public Vector2 GetPointOnBase(float x, bool topSide) {
if (x <= width) { return new Vector2(x+(topSide ? 0 : -width), (topSide ? r : -r)); }
else if (x > width) { return new Vector2(x+(topSide ? 0 : -width*2-r), CornerByAxisPoint(x-width+(topSide ? 0 : -r), topSide)); }
else throw pointOut;
}
public Vector2 GetPointOnLegs(float y, bool rightSide) {
if (y <= height) { return new Vector2(0, y * (rightSide ? -1 : 1)); }
else if (y > height) { return new Vector2(CornerByAxisPoint(y-height, rightSide)+(rightSide ? -r : r), (rightSide ? -y : y)); }
else throw pointOut;
}
public Vector2 GetPoint(float p) {
if (p <= radWidth) {
return GetPointOnBase(p, true)+Vector2.right*r; }
else if (p > radWidth && p <= radWidth + radHeight) {
return GetPointOnLegs(p - radWidth, true)+Vector2.right*fullWidth; }
else if (p > radWidth + radHeight && p <= radWidth*2 + radHeight) {
return GetPointOnBase(p - (radWidth + radHeight), false)+Vector2.right*radWidth+Vector2.down*height;
} else if (p > radWidth*2 + radHeight && p <= radWidth*2 + radHeight*2) {
return GetPointOnLegs(p - (radWidth*2 + radHeight), false)+Vector2.down*height;
} else throw pointOut;
}
public Vector2 Interpolate(float t) => GetPoint(Mathf.Lerp(0f, perimeter, t));I'm making rounded rectangle. Either pieces are missing or lerp seems ignoring corners. Using unit circle x^2+y^2=r^2. Might be sqrt() or comparison conditions
