I've implemented an Objective-C function to display the "height" of a half-sphere, with "1.0" being "full-height" and "0.0" being "no-height"
The sphere currently has a few parameters:
- Center (x,y: real coordinates)
- Radius (r: real value)
These values are currently being drawn in a texture with an independent size, provided that the minimum coordinate is (0,0) and the maximum is (1,1), which could be represented by a mathematical function like f(px, py)
, where px
and py
are the points where I want the height of the given half-sphere.
Now, I want to add two more parameters to the sphere, a "x-stretch" and a "y-stretch" (we can call them xs
and ys
), where I could deform the sphere in the X or Y axis.
The current function is like this (provided that the values of "x", "y" and "r" are known):
- $ d(px, py) = \sqrt{(x-px)^2 + (y+py)^2} $ as being the distance from the center to the given point
- $ f(px, py) = \sqrt{1 - \frac{d(px, py)}{r}} $ as being the value (from 0.0 to 1.0) of the half-sphere height.
Note that f
is only calculated if d
is smaller than the radius(r
)
I want a way to add xs
and ys
in the above equations, but I don't really know how to get around that.
If it helps, here's the Objective-C function that "solves" the above problem:
- (CGFloat)sphereValueAtPoint:(NSPoint)point center:(NSPoint)sphereCenter radius:(CGFloat)radius { CGFloat dist; dist = sqrt(pow(point.x - sphereCenter.x,2) + pow(point.y - sphereCenter.y, 2)); if (dist > radius) return(0.0); return(sqrt(1 - pow(dist/radius, 2))); }
I've also got a faster, optimized function, but it is not as easy to understand.
EDIT: Note that when xs
and ys
are both equal to 1.0
, no scaling should be done. If either are GREATER than 1.0
, the texture is stretched and if they are between 1.0
and 0.0
, the texture is compressed. You can assume that xs
and ys
are always greater than 0.0
.
EDIT2: As requested, I'm adding some images for clarification. White is 1.0, Black is 0.0 and the gray is in between (check tooltips).