How to Find a Point Where a Circle and Line with 1/0 Slope Intersect - python-3.x

I'm writing a simple 2D top-down game in Python 3 using tkinter. All the collidable objects are either circles/arcs or lines. I wrote the following method to detect when a circle hits a line:
I am using the formulas y = mx + b and r^2 = (x-h)^2 + (y-k)^2
def CheckHitCToL(self, LX0, LY0, LX1, LY1, CX0, CX1, Tab):
try:
H = self.Creatures[Tab].X
K = self.Creatures[Tab].Y
R = abs((CX0 - CX1) / 2)
M = (LY0 - LY1) / (LX0 - LX1)
B = M * (-LX1) + LY1
QA = (M * M) + 1
QB = (-H - H) + (((B - K) * M) * 2)
QC = (H * H) + ((B - K) * (B - K)) - (R * R)
X = (- QB + sqrt((QB * QB) - (4 * QA * QC))) / (2 * QA)
Y = (M * X) + B
if ((X <= LX0 and X >= LX1) or (X >= LX0 and X <= LX1)) and ((Y <= LY0 and Y >= LY1) or (Y >= LY0 and Y <= LY1)):
return True
else:
return False
except:
return False
My problem is when you have a vertical line, M (Or the slope) is (LY0 - LY1) / 0. (This is because the slope is equal to rise/run, and vertical lines don't have a run, just a rise) Which of course returns an error, caught by try except, which then informs my movement method that no collision has taken place. Of course I can simply move the "try:" down a few lines, but it's still going to throw an error. How can I adapt this program to not throw an error when working with a vertical line?

Well, the most obvious method would involve using if( (LX0 - LX1)==0 ) and doing this case separately. In such cases, you need to check whether distance between LX0 and CX0 is equal to the radius of circle.

You can use another forms of line equation -
implicit A*x + B*y + C = 0
or parametric x = LX0 + t * (LX1 - LX0), y = LY0 + t *(LY1 - LY0)
with appropriate modification of calculations

Related

Mathematics - Distribute a list of numbers over an interval

My problem is simple.
I am searching a mathematical function to distribute number over an interval.
For example I have this list :
[2; 4; 9; 14]
And in my case I wish
2 -> 1 = f(2)
14 -> 20 = f(14)
4 -> f(4) = ?
9 -> f(9) = ?
This is just an example I am searching for f(x).
Someone would have any idea ?
Thanks for advance ! :)
If you want a linear function, then:
f(x) = lowerFunc + (x - lowerX) * (upperFunc - lowerFunc) / (upperX - lowerX),
where:
lowerFunc: function value at the lower end
upperFunc: function value at the upper end
lowerX: x parameter at the lower end
upperX: x parameter at the upper end.
For your example:
f(x) = 1 + (x - 2) * (20 - 1) / (14 - 2)
= 1 + (x - 2) * 19/12
f(2) = 1
f(4) = 4.1666
f(9) = 12.08333
f(14) = 20

Intersect of two planes - divide by zero

I have following alghoritm to find line intersection of two planes:
public static function getIntersectOf2Planes ( self $P1 , self $P2 )
{
/* Line equation in parametric form:
x = x0 + t*a
y = y0 + t*b
z = z0 + t*c
*/
$x0 = ( $P1->B * $P2->D - $P2->B * $P1->D ) / ( $P1->A * $P2->B - $P2->A * $P1->B ) ;
$a = ( $P1->B * $P2->C - $P2->B * $P1->C );
$y0 = ( $P2->A * $P1->D - $P1->A * $P2->D ) / ( $P1->A * $P2->B - $P2->A * $P1->B ) ;
$b = ( $P2->A * $P1->C - $P1->A * $P2->C );
$z0 = 0;
$c = ( $P1->A * $P2->B - $P2->A * $P1->B );
$IntersectionLine = new Line3D( $x0, $a, $y0, $b, $z0, $c );
return $IntersectionLine;
}
and it works fine, but to compute $x0 and $y0 i have to divide by:
( $P1->A * $P2->B - $P2->A * $P1->B )
and in some cases, the value of this expression is equal to zero, so I get an "dividing by zero" error :(
What should I do in this case?
I know, that the case when this expression is equal to zero, doesn't mean that there is no intersection, because it's happen when I have planes perpendicular to one of the axies.
For example for:
Plane1:
A = 0
B = 0
C = 100
D = 0
Plane2:
A = 50
B = 0
C = 0
D = -250
so the equation of line should exists.
PS I wrote this code with a view to:
https://math.stackexchange.com/questions/2766615/line-by-two-planes-intersection?noredirect=1#comment5706281_2766615
In short, you have to implement the intersection algorithm for the case when (a1*b2 - a2*b1) = 0 (ie. when the planes are not independent when you set z=0).
To expand on that, first we need to understand how you got this far. First let us write down the equation of two planes:
a1x + b1y + c1z + d1 = 0
and
a2x + b2y + c2z + d2 = 0
When two planes intersect, the intersection is a line. So, the most usual way to solve that is by finding a point first on such a line and then figuring out its orientation (a, b, c) in your case. The orientation is a straight forward cross product. The intersection point is typically calculated by setting one of the co-ordinates to 0 and then solving the 2 linear equations that you get. In your code, this is done by setting:
z = 0
But this only works when the equations
a1x + b1y + d1 = 0 and a2x + b2y + d2 = 0
are capable of giving a solution for x and y, which is not the case when a1b2-a2b1=0. So In such cases, you can solve the same by setting either x or y to 0 which again gives two linear equations that you can solve to obtain a point on the line. Then you can compute the parametric form much like how you did. For example (setting y to 0):
x0 = (c1d2 - c2d1)/(a1c2 - a2c1)
y0 = 0
z0 = (a2d1 - a1d2)/(a1c2 - a2c1)
But for this to be a defined value you need to have (a1c2 - a2c1) to be non-zero. Does this help?

Intersection between line and sphere

I need to find the intersection between a line and a sphere defined by the following equations
Line: P = P0 + tv, where P0 is the eye of a camera and v is the direction of a ray.
Sphere: (P - Pc)^T (P - Pc) = r^2, where Pc is the center of the sphere and r is the radius
How would I solve for t?
I assume that v is normalized.
The point of closest approach of the line to the center of the sphere occurs at t1:
v . (P0 + t1 v - Pc) = 0
v . (Pc - P0) = t1
At t1 the distance from the line to the center of the sphere is
h = sqrt((Pc - P0)^2 - t1^2)
The intersection with the surface of the sphere will occur (if it occurs at all) at t1 +/- td, where
td^2 + h^2 = r^2
td^2 = r^2 - h^2
= r^2 - (Pc - P0)^2 + t1^2
= r^2 - (Pc - P0)^2 + (v . (Pc - P0))^2
First change the coordinates to a system on the sphere center. P0 -> P0-Pc
Now you have P0=(x0,y0,z0) and the ray coordinates as
x = x0+t*vx
y = y0+t*vy
z = z0+t*vz
and make sure ǁvx,vy,vzǁ=1
The equation of the sphere is x^2+y^2+z^2-R^2 = 0 which is expanded as
t^2 + 2*(vx*x0+vy*y0+vz*z0)*t + (x0^2+y0^2+z0^2-R^2) = 0
t^2 + 2*B*t+C =0
B = vx*x0+vy*y0+vz*z0
C = x0^2+y0^2+z0^2-R^2
t = +√(B^2-C)/2-B
t = -√(B^2-C)/2-B
Which gives the two solutions, p=Pc+P0+v*t for the two t computed.

Please explain this color blending mode formula so I can replicate it in PHP/ImageMagick

I've been trying to use ImageMagick to replicate Photoshops Colour Blend Mode. I found the following formulas in an online guide but I don't know what they mean. Do I just need to swap certain channels?
A while ago I reversed engineered Photoshop blending modes.
Have a look here:
http://www.kineticsystem.org/?q=node/13
And here below the code I use to convert between HSY (Hue, Saturation, Luminosity) to and from RGB (Red, Green, Blue). Photoshop use something called Hexacones to calculate the saturation.
Giovanni
/**
* This is the formula used by Photoshop to convert a color from
* RGB (Red, Green, Blue) to HSY (Hue, Saturation, Luminosity).
* The hue is calculated using the exacone approximation of the saturation
* cone.
* #param rgb The input color RGB normalized components.
* #param hsy The output color HSY normalized components.
*/
public static void rgbToHsy(double rgb[], double hsy[]) {
double r = Math.min(Math.max(rgb[0], 0d), 1d);
double g = Math.min(Math.max(rgb[1], 0d), 1d);
double b = Math.min(Math.max(rgb[2], 0d), 1d);
double h;
double s;
double y;
// For saturation equals to 0 any value of hue are valid.
// In this case we choose 0 as a default value.
if (r == g && g == b) { // Limit case.
s = 0d;
h = 0d;
} else if ((r >= g) && (g >= b)) { // Sector 0: 0° - 60°
s = r - b;
h = 60d * (g - b) / s;
} else if ((g > r) && (r >= b)) { // Sector 1: 60° - 120°
s = g - b;
h = 60d * (g - r) / s + 60d;
} else if ((g >= b) && (b > r)) { // Sector 2: 120° - 180°
s = g - r;
h = 60d * (b - r) / s + 120d;
} else if ((b > g) && (g > r)) { // Sector 3: 180° - 240°
s = b - r;
h = 60d * (b - g) / s + 180d;
} else if ((b > r) && (r >= g)) { // Sector 4: 240° - 300°
s = b - g;
h = 60d * (r - g) / s + 240d;
} else { // Sector 5: 300° - 360°
s = r - g;
h = 60d * (r - b) / s + 300d;
}
y = R * r + G * g + B * b;
// Approximations erros can cause values to exceed bounds.
hsy[0] = h % 360;
hsy[1] = Math.min(Math.max(s, 0d), 1d);
hsy[2] = Math.min(Math.max(y, 0d), 1d);
}
/**
* This is the formula used by Photoshop to convert a color from
* HSY (Hue, Saturation, Luminosity) to RGB (Red, Green, Blue).
* The hue is calculated using the exacone approximation of the saturation
* cone.
* #param hsy The input color HSY normalized components.
* #param rgb The output color RGB normalized components.
*/
public static void hsyToRgb(double hsy[], double rgb[]) {
double h = hsy[0] % 360;
double s = Math.min(Math.max(hsy[1], 0d), 1d);
double y = Math.min(Math.max(hsy[2], 0d), 1d);
double r;
double g;
double b;
double k; // Intermediate variable.
if (h >= 0d && h < 60d) { // Sector 0: 0° - 60°
k = s * h / 60d;
b = y - R * s - G * k;
r = b + s;
g = b + k;
} else if (h >= 60d && h < 120d) { // Sector 1: 60° - 120°
k = s * (h - 60d) / 60d;
g = y + B * s + R * k;
b = g - s;
r = g - k;
} else if (h >= 120d && h < 180d) { // Sector 2: 120° - 180°
k = s * (h - 120d) / 60d;
r = y - G * s - B * k;
g = r + s;
b = r + k;
} else if (h >= 180d && h < 240d) { // Sector 3: 180° - 240°
k = s * (h - 180d) / 60d;
b = y + R * s + G * k;
r = b - s;
g = b - k;
} else if (h >= 240d && h < 300d) { // Sector 4: 240° - 300°
k = s * (h - 240d) / 60d;
g = y - B * s - R * k;
b = g + s;
r = g + k;
} else { // Sector 5: 300° - 360°
k = s * (h - 300d) / 60d;
r = y + G * s + B * k;
g = r - s;
b = r - k;
}
// Approximations erros can cause values to exceed bounds.
rgb[0] = Math.min(Math.max(r, 0d), 1d);
rgb[1] = Math.min(Math.max(g, 0d), 1d);
rgb[2] = Math.min(Math.max(b, 0d), 1d);
}
Wikipedia has a good article on blend modes
http://en.wikipedia.org/wiki/Blend_modes
They give formulas for Multiply, Screen and Overlay modes.
Multiply
Formula: Result Color = (Top Color) * (Bottom Color) /255
Screen
Formula: Result Color = 255 - [((255 - Top Color)*(255 - Bottom Color))/255]
Overlay
Formula: Result Color = if (Bottom Color < 128)
then (2 * Top Color * Bottom Color / 255)
else (255 - 2 * (255 - Top Color) * (255 - Bottom Color) / 255)
A is the Foreground pixel, B is the Background pixel, C is the new pixel. H is the Hue value for each pixel, S is the Saturation value, L is the Luminance value, and Y is the Brightness value. (Not sure what the difference is between luminance and brightness though.
Anyway, in the first example the Hue(H) and Saturation(S) values of the new pixel(C) are copied from the Foreground pixel(A) while the Brightness(Y) value of the new pixel is taken from the Luminance(L) value of the Background(B) pixel.
These color blending formulas are quite tricky if you need to incorporate also the alpha channel. I was not able to reproduce the blending of Photoshop, but Gimp works like this:
Color mix_hsv(
ColorMixMode::Enum mode, // blending mode
Color cd, // destination color (bottom pixel)
Color cs) // source color (top pixel)
{
// Modify the source color
float dh, ds, dv; // destination hsv
float sh, ss, sv; // source hsv
cd.GetHsv(dh, ds, dv);
cs.GetHsv(sh, ss, sv);
switch (mode) {
case HUE: cs.InitFromHsv(sh, ds, dv); break;
case SATURATION: cs.InitFromHsv(dh, ss, dv); break;
case COLOR: cs.InitFromHsv(sh, ss, dv); break;
case LUMINOSITY: cs.InitFromHsv(dh, ds, sv); break;
}
cs.A = std::min(cd.A, cs.A);
// Blend the modified source color onto the destination color
unsigned char cd_A_orig = cd.A;
cd = mix(NORMAL, cd, cs); // normal blending
cd.A = cd_A_orig;
return cd;
}
If you use premultiplied alpha, don't forget to correctly handle it in the above code. I was not able to find the code for blending in Gimp's source code, but the resulting images are very similar.
Photoshop's color blending is clearly different, so if anyone finds a way to implement it, please let us all know :o)
Miso

Partition line into equal parts

This is a geometry question.
I have a line between two points A and B and want separate it into k equal parts. I need the coordinates of the points that partition the line between A and B.
Any help is highly appreciated.
Thanks a lot!
You just need a weighted average of A and B.
C(t) = A * (1-t) + B * t
or, in 2-D
Cx = Ax * (1-t) + Bx * t
Cy = Ay * (1-t) + By * t
When t=0, you get A.
When t=1, you get B.
When t=.25, you a point 25% of the way from A to B
etc
So, to divide the line into k equal parts, make a loop and find C, for t=0/k, t=1/k, t=2/k, ... , t=k/k
for(int i=0;i<38;i++)
{
Points[i].x = m_Pos.x * (1 - (i/38.0)) + m_To.x * (i / 38.0);
Points[i].y = m_Pos.y * (1 - (i/38.0)) + m_To.y * (i / 38.0);
if(i == 0 || i == 37 || i == 19) dbg_msg("CLight","(%d)\nPos(%f,%f)\nTo(%f,%f)\nPoint(%f,%f)",i,m_Pos.x,m_Pos.y,m_To.x,m_To.y,Points[i].x,Points[i].y);
}
prints:
[4c7cba40][CLight]: (0)
Pos(3376.000000,1808.000000)
To(3400.851563,1726.714111)
Point(3376.000000,1808.000000)
[4c7cba40][CLight]: (19)
Pos(3376.000000,1808.000000)
To(3400.851563,1726.714111)
Point(3388.425781,1767.357056)
[4c7cba40][CLight]: (37)
Pos(3376.000000,1808.000000)
To(3400.851563,1726.714111)
Point(3400.851563,1726.714111)
which looks fine but then my program doesn't work :D.
but your method works so thanks

Resources