Geometric basis for computing-[Use of point products and cross products]

Source: Internet
Author: User

Computational ry is a big part of the algorithm competition, and the cross product is the foundation of computers.

First, the cross product is used to calculate the cross product between vectors, so we can define the vector and the operator overload of the vector.

struct Point{    double x,y;    Point(double x=0,double y=0):x(x),y(y) {}};typedef Point Vector;Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y); }Vector operator - (Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); }Vector operator * (Vector A,double p) { return Vector(A.x*p,A.y*p); }Vector operator / (Vector A,double p) { return Vector(A.x/p,A.y/p); }bool operator < (const Point& a,const Point& b){    return a.x<b.x || (a.x==b.x && a.y<b.y);}int dcmp(double x)  //{    if(fabs(x)<esp) return 0;    else return x<0?-1:1;}bool operator == (const Point& a,const Point& b){    return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y)==0;}


First, we will introduce some definitions under two-dimensional coordinates:

Point: A (x1, Y1), B (X2, Y2)

Vector: vector AB = (x2-X1, Y2-Y1) = (x, y );

Vector modulo | AB | = SQRT (x * x + y * y );


Dot Product of the vector: The result is X1 * X2 + Y1 * Y2.

The result of the dot product is a numerical value.

The Set meaning of dot product: When we take vector A as a vertical line to vector B, then | A | * Cos (A, B) is the projection of a on vector B, point product is the projection of one vector on another vector multiplied by another vector. And meet the exchange law

Application: The angle between two vectors can be obtained based on the set meaning. Cos (a, B) = (vector A * vector B)/(| A | * | B |) = x1 * X2 + Y1 * y2/(| A | * | B |)



Cross Product of the vector: The result is X1 * y2-x2 * Y1

The result of the cross product is also a vector, which is perpendicular to the plane formed by the vectors A and B. If it is regarded as a three-dimensional coordinate, it is on the Z axis, and the result above is its model.

Direction determination: Right Hand timing, (right hand half grip, thumb vertical up, four fingers right vector a hold B, thumb direction is the direction of the Cross Product)

The result is that a and B form the area of the parallelogram for the adjacent edge.

2: The result is positive and negative. Sin (a, B) indicates that it is related to its angle. The angle greater than 180 ° indicates a negative value.

3: The cross product does not meet the exchange law.

Application:

1: Determine the clockwise relationship between two vectors through positive and negative results

If a x B> 0 indicates that A is clockwise in B

If a x B <0 indicates that A is in the clockwise direction of B

If a x B = 0, it indicates that a is in the same line of B, but it is not sure whether the direction is the same


2: Determine the line direction, can be converted to judge the third point in the first two form a straight line along the clockwise direction, and then judge the turn.

3: Determine the side of a line by using the same method above.

4: Determine whether the vertex is in the online segment. You can use the cross-multiplication method to determine whether the vertex is in the same line, and then determine whether the vertex is in the same line.

5: Determine whether two straight lines want to be handed in (cross-site experiment)

Based on the judgment point on the side of the line, we can determine that the two points on a line segment are on both sides of the other line segment. Of course, this is not enough, because we find that this can only make the line want to interwork, instead of line segments, we have to make the same judgment on another line segment.


Code:

/// Calculate the dot product, the vector length, and the vector angle double dot (vector A, vector B) {return. x * B. X +. y * B. y;} double length (vector A) {return SQRT (dot (A, A);} double angle (vector A, vector B) {return ACOs (dot (, b)/length (a)/length (B);} // calculate the cross product. The vector rotates counter-clockwise. Do you want to cross the double cross (vector A, vector B) {return (. x * B. y-A.y * B. (x);} double area2 (vector A, vector B, vector c) {return cross (B-A, C-A);} vector rotate (vector A, double rad) {return vector (. x * Cos (RAD)-. y * sin (RAD),. x * sin (RAD) +. y * Cos (RAD);} bool converxline (vector A, vector B, vector C, vector d) {// collinearity or parallel if (area2 (A, B, c) = 0 & area2 (a, B, d) = 0) | area2 (a, B, c) * area2 (a, B, d)> 0 | area2 (c, d, A) * area2 (c, d, B)> 0) return false; else return true ;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.