// Calculates the first intersection point of the sphere and the ray with // the direction d. int calculateIntersectionPoint(cv::Vec3d d) { double t1 = (- (d.t() * (e - c))[0] + sqrt(discriminant(d))) / (d.t() * d)[0]; double t2 = (- (d.t() * (e - c))[0] - sqrt(discriminant(d))) / (d.t() * d)[0]; return std::min(t1, t2); }
int main(){ double a;//created variables double b; double c; double x1; double x2; cout<<"please input values a b c to solve the function of the form ax^2+bx+c=0\na=";//prompt for user input of variables a b and c cin>>a; cout<<"b="; cin>>b; cout<<"c="; cin>>c; if (a==0&b==0&c==0){//if statement that outputs that every possible number is a root cout<<"every real and complex number is a root\n"; keep_window_open();//stops the program return 0;} if (a==0&b==0&c!=0){//if statement that outputs the equation is inconsistent in other words it is not of the form ax^2+bx+c and has no roots cout<<"the equation is inconsistent\n"; keep_window_open();//stops the program return 0;} if (discriminant(a,b,c)<0){//if statement that outputs the possible roots are complex cout<<"the roots are complex\n"; keep_window_open();//stops the program return 0;} if (a==0&b!=0){//if statement that says there is only one root cout<<"there is one root\n"; } if(discriminant(a,b,c)==0){//if statement that outputs there are two of the same roots cout<<"there is one double root\n"; } x1=(-b+sqrt((b*b)-(4*a*c)))/(2*a);//calcution of the roots x2=(-b-sqrt((b*b)-(4*a*c)))/(2*a); double r1=residual(a,b,c,x1);//use of the function to calculate the residual of roots x1 and x2 double r2=residual(a,b,c,x2); cout<<"root1:"<<x1<<"\nroot2:"<<x2<<"\nresidual1:"<<r1<<"\nresidual2:"<<r2<<"\n"; //output of the roots and residuals keep_window_open(); return 0; }
int solve_quadratic_eq(const double (*coeffs)[3], double (*roots)[4]) { // find discriminant and then calculate roots double d = discriminant(coeffs); int retCode = 0; if (d > zero_tol) // two real roots { double d1 = (-(*coeffs)[1]+sqrt(d))/(2.0* (*coeffs)[0]); double d2 = (-(*coeffs)[1]-sqrt(d))/(2.0* (*coeffs)[0]); (*roots)[0] = d1; (*roots)[1] = d2; } else if (d < -zero_tol) // complex roots { double r = (-(*coeffs)[1])/(2.0*(*coeffs)[0]); double w = (sqrt(d)/(2.0*(*coeffs)[0])); (*roots)[0] = r; (*roots)[1] = w; // first root is x1 = r + iw, real part is roots[0], imaginary is roots[1] (*roots)[2] = r; (*roots)[3] = -w; // second root is x2 = r - iw, real part is roots[2], imaginary is roots[2] retCode = 1; } else // two identical real roots { double r = (-(*coeffs)[1])/(2.0*(*coeffs)[0]); (*roots)[0] = r; (*roots)[1] = r; } return retCode; }
//Main int main(void) { double a, b, c, disk; //Variables for user input a, b and c. Variable for discriminant. double posRoot = 0, negRoot = 0; //Variables for storing roots //Prompt user for a, b, c printf("Insert the value of a, b & c\n"); scanf("%lf%lf%lf",&a,&b,&c); //Calculate the discriminant disk = discriminant(a, b, c); //Print the results if (disk == 0) { //If the discriminant equals 0, there is one root. posRoot = solveRootPos(a, b, disk); printf("The first and only root is: %lf\n", posRoot); } else if (disk > 0) {//If the discriminant is above 0, there are two roots posRoot = solveRootPos(a, b, disk); negRoot = solveRootNeg(a, b, disk); printf("The first root is: %lf and the second root is: %lf\n", posRoot, negRoot); } else//If the discriminant does not equal 0, nor is above 0, there are no roots! printf("Error! Discriminant is below zero!\n"); return 0; }
Real time_hit(Circle c1,Circle c2){ Circle d; Vector root; d.x.x=c1.x.x-c2.x.x; d.x.y=c1.x.y-c2.x.y; d.v.x=c1.v.x-c2-v.x; d.v.y=c1.v.y-c2.v.y; d.r=c1.r+c2.r; Real a=Sqr(d.v.x)+Sqr(d.v.y),b=2.0*(d.v.x*d.x.x+d.v.y*d.x.y),c=Sqr(d.x.x)+Sqr(d.x.y)-d.r; if((d.v.x==0.0&&d.v.y==0.0)||(discriminant(a,b,c)){ return INF; }else{
CTEST(test_suite, negative_discriminant) { const double a = 4; const double b = 2; const double c = 3; double test = discriminant(a, b, c); const double expected_discriminant = -44; ASSERT_DBL_NEAR(expected_discriminant, test); }
CTEST(test_suite, positive_discriminant) { const double a = 4; const double b = 6; const double c = 1; double test = discriminant(a, b, c); const double expected_discriminant = 20; ASSERT_DBL_NEAR(expected_discriminant, test); }
CTEST(test_suite, zero_discriminant) { const double a = 3; const double b = 6; const double c = 3; double test = discriminant(a, b, c); const double expected_discriminant = 0; ASSERT_DBL_NEAR(expected_discriminant, test); }
struct maybe_point sphere_intersection_point(struct ray r, struct sphere s) { double a = dot_vector(r.dir, r.dir); double b = 2 * dot_vector(difference_point(r.p, s.center), r.dir); double c = dot_vector(difference_point(r.p, s.center), difference_point(r.p, s.center)) - (s.radius * s.radius); double disc = discriminant(a, b, c); double pos_result = pos_quadratic(a, b, c); double neg_result = neg_quadratic(a, b, c); double result; if (disc < 0) { return create_maybe_point(0, create_point(0.0, 0.0, 0.0)); } if (pos_result > 0 && neg_result > 0) { if (pos_result < neg_result) { result = pos_result; } else { result = neg_result; } } else if (pos_result > 0) { result = pos_result; } else if (neg_result > 0) { result = neg_result; } else { result = -1; } if (result > 0) { /*point is valid*/ return create_maybe_point(1, translate_point(r.p, scale_vector(r.dir, result))); } else { /* point is invalid*/ return create_maybe_point(0, translate_point(r.p, scale_vector(r.dir, result))); } }
//Calculates the square roots of the given quadratic equation void roots(int a,int b,int c) { float disc; disc=discriminant(a,b,c); float root1,root2; if(disc<0) printf("it has imaginary square roots \n"); else if(disc==0) { printf("The roots are \n%.2f\n%.2f\n",((float)-b/(2*a)),((float)-b/(2*a))); } else{ root1=(-b+squareRoot(disc))/(2*a); root2=(-b-squareRoot(disc))/(2*a); printf("the square roots are : \n"); printf("%.2f\n%.2f \n",root1,root2); } }
std::set<Argument> SolveQuadraticEquation( Argument const& origin, Value const& a0, Derivative<Value, Argument> const& a1, Derivative<Derivative<Value, Argument>, Argument> const& a2) { using Derivative1 = Derivative<Value, Argument>; using Discriminant = Square<Derivative1>; std::set<Argument> solutions; // This algorithm is after section 1.8 of Accuracy and Stability of Numerical // Algorithms, Second Edition, Higham, ISBN 0-89871-521-0. static Discriminant const discriminant_zero{}; // Use compensated summation for the discriminant because there can be // cancellations. DoublePrecision<Discriminant> discriminant(a1 * a1); discriminant.Increment(-4.0 * a0 * a2); if (discriminant.value == discriminant_zero && discriminant.error == discriminant_zero) { // One solution. solutions.insert(origin - 0.5 * a1 / a2); } else if (discriminant.value < discriminant_zero || (discriminant.value == discriminant_zero && discriminant.error < discriminant_zero)) { // No solution. } else { // Two solutions. Compute the numerator of the larger one. Derivative1 numerator; static Derivative1 derivative_zero{}; if (a1 > derivative_zero) { numerator = -a1 - Sqrt(discriminant.value + discriminant.error); } else { numerator = -a1 + Sqrt(discriminant.value + discriminant.error); } solutions.insert(origin + numerator / (2.0 * a2)); solutions.insert(origin + (2.0 * a0) / numerator); } return solutions; }
int main() { cv::Mat m(pictureWidth, pictureWidth, CV_8UC1); for (int i = 0; i < m.rows; i++) { for (int j = 0; j < m.cols; j++) { // Get a new direction using a bit of magic for coordinates. cv::Vec3d d = e + cv::Vec3d(j - 100, 100 - i, 50); if (discriminant(d) >= 0) { // The ray intersects the sphere. double t = calculateIntersectionPoint(d); // Sphere surface normal. cv::Vec3d n = e + t * d - c; // Direction from the sphere to light. cv::Vec3d onLight = l - (e + t * d); cv::Vec3d h = onLight - (e + t * d); // Blinn-Phong model. double shininess = 100.0; double color = std::max(0.0, cosVec3d(n, onLight)) * 255 + 35 * pow(cosVec3d(n, h), shininess); // Ugly hack to prevent overflow. if (color > 255) { color = 255; } m.at<char>(i, j) = color; } else { // The ray doesn't intersect the sphere. m.at<char>(i, j) = 0; } } } cv::imwrite("hw02.png", m); return 0; }
static CYTHON_INLINE struct ZZ* ZZX_discriminant(struct ZZX* x, int proof) { ZZ* d = new ZZ(); discriminant(*d, *x, proof); return d; }
double neg_quadratic(double a, double b, double c) { return(((-b) - sqrt(discriminant(a, b, c)))/(2*a)); }