• 代码说明题目测试平台编程语言： java操作系统： windows实验结果关于直线与直线的交点的求解测试代码public class Exp2 {public static void main(String[] args) {// 线-线 交点测试Line line1 = new Line(7, 10, ...
机器人技术第二次作业.代码说明题目测试平台编程语言： java操作系统： windows实验结果关于直线与直线的交点的求解测试代码public class Exp2 {public static void main(String[] args) {// 线-线 交点测试Line line1 = new Line(7, 10, 3);Line line2 = new Line(5, 5, 9);String string = line1.intersection(line2).toString();System.out.println(line1.toString());System.out.println(line2.toString());System.out.println("-------------------------");System.out.println(string);}}结果关于直线与圆的交点的求解测试代码public class Exp2 {public static void main(String[] args) {// 线-园 交点测试Point center = new Point(0, 0);Circle circle = new Circle(center, 1);Line line = new Line(2, 0, -1);Point[] intersections = line.intersectionCircle(circle);System.out.println(line.toString());System.out.println(circle.toString());System.out.println("-------------------------");System.out.println(intersections[0].toString());System.out.println(intersections[1].toString());}}结果关于直线与矩形交点的求解测试代码public class Exp2 {public static void main(String[] args) {// 线-矩 形交点测试Point point = new Point(0, 5);Point point1 = new Point(5, 0);Rec rec = new Rec(point, point1);Line line = new Line(-1, 1, -1);Point[] intersections = line.intersectionRec(rec);System.out.println(rec.toString());System.out.println(line.toString());System.out.println("-------------------------");System.out.println(intersections[0].toString());System.out.println(intersections[1].toString());}}结果代码package robot.week2;/*** @author liteng* @version 0.0*/// 点类class Point {private float x;private float y;Point(float x, float y) {this.x = x;this.y = y;}public float getX() {return x;}public void setX(float x) {this.x = x;}public float getY() {return y;}public void setY(float y) {this.y = y;}@Overridepublic String toString() {return "Point{ ( " + x + " ," + y + " ) }";}}// 圆类class Circle {private Point center;private float radius;// (x - center.x)^2 + (y - center.y)^2 = radius^2Circle(Point center, float radius) {this.center = center;this.radius = radius;}Circle(float x, float y, float radius) {this.center = new Point(x, y);this.radius = radius;}public Point getCenter() {return center;}public float getRadius() {return radius;}@Overridepublic String toString() {return "Circle{ ( " + center.getX() + ", " + center.getY() + " ), " + radius + "}";}}// 矩形类class Rec {private Point point1; // 左上角点private Point point2; // 右下角点Rec(Point point1, Point point2) {this.point1 = point1;this.point2 = point2;}Rec(float x1, float y1, float x2, float y2) {this.point1 = new Point(x1, y1);this.point2 = new Point(x2, y2);}public Line getLine(int property) {// property// |- LEFT = 1;// |- RIGHT = 2;// |- TOP = 3;// |- BOTTOM = 4;switch (property) {// TODO to be modifiedcase 1:return new Line(1, 0, -point1.getX());case 2:return new Line(1, 0, -point2.getX());case 3:return new Line(0, 1, -point1.getY());case 4:return new Line(0, 1, -point2.getY());default:return null;}}public Point getPoint1() {return point1;}public Point getPoint2() {return point2;}@Overridepublic String toString() {return "Rec{ ( " + point1.getX() + ", " + point1.getY() + " ), ( " + point2.getX() + ", " + point1.getY() + " ) }"+ "\n   { ( " + + point1.getX() + ", " + point2.getY() + " ), ( " + point2.getX() + ", " + point2.getY() + " ) }";}}// 线类class Line {float a;float b;float c;// ax + by + c = 0Line(float a, float b, float c) {this.a = a;this.b = b;this.c = c;}Line(Point point1, Point point2) {this.a = point2.getY() - point1.getY();this.b = point1.getX() - point2.getX();this.c = point2.getX() * point1.getY() - point1.getX() * point2.getY();// TODO add exception to diff point1 & point2}public Point xGetPoint(float x) {// 已知直线上一点x坐标，求y坐标if (this.a == 0) {// 平行于x轴float y =  -this.c / this.b;return new Point(x, y);}else if (this.b == 0) {return new Point(x, 0); // 若平行于y轴，默认返回与x轴交点}else {float y = (-this.c - this.a * x) / b;return new Point(x, y);}}public Point yGetPoint(float y) {// 已知直线上一点y坐标，求x坐标if (this.a == 0) {return new Point(0, y); // 直线与x轴平行}else if (this.b == 0) {float x = -this.c / this.a;return new Point(x, y);}else {float x = (-this.c - this.b * y) / a;return new Point(x, y);}}public Point intersectionLine(Line line) {if (this.a != 0 && this.b != 0 && line.a != 0 && line.b != 0) { // 直线不平行于坐标轴float k1 = -this.a / this.b;float k2 = -line.a / line.b;float epsilon = 0.05f; // 判断两条直线是否平行阈值if (Math.abs(k1 - k2) < epsilon) { // 两直线平行return null;}else { // 两直线不平行float x = -((this.b * line.c) / line.b - this.c) / (this.a - (line.a * this.b) / line.b);float y = -((this.a * line.c) / line.a - this.c) / (this.b - (this.a * line.b) / line.a);Point point = new Point(x, y);return point;}}else { // 含有平行于坐标轴的直线if ((this.a == 0 && line.a == 0) || (this.b == 0 && line.b == 0)) {// 若两直线平行return null;}else if (this.a == 0 || this.b == 0) {// 若第一条直线平行于坐标轴if (this.a == 0) {// 平行于x轴float y = -this.c / this.b;return line.yGetPoint(y);}else if (this.b == 0) {// 平行于y轴float x = -this.c / this.a;return line.xGetPoint(x);}}else if (line.a == 0 || line.b == 0) {// 若第二条直线平行于坐标轴if (line.a == 0) {// 平行于y轴float y = -line.c / line.b;return this.yGetPoint(y);}else if (line.b == 0) {// 平行于x轴float x = -line.c / line.a;return this.xGetPoint(x);}}}return null;}public float distPoint(Point point) {return (float) (Math.abs(a * point.getX() + b * point.getY() + c) / (Math.sqrt(a * a + b * b)));}public Point[] intersectionCircle(Circle circle) {Point[] points = null;float dist = distPoint(circle.getCenter());final float epsilon = 0.05f;if (Math.abs(dist - circle.getRadius()) < epsilon) {// 相切points = new Point[1];float angle = (float) Math.atan(-this.a / this.b);points[0] = new Point((float)(circle.getCenter().getX() + circle.getRadius() * Math.cos(Math.PI + angle)),(float)(circle.getCenter().getX() + circle.getRadius() * Math.sin(Math.PI + angle)));return points;}else if ((dist - circle.getRadius()) > epsilon) {// 直线与圆相离return points;}else if (dist - circle.getRadius() < 0) {// 相交if ((this.b - 0f) > epsilon) {float k = -this.a / b;float b = -this.c / this.b;float c = circle.getCenter().getX();float d = circle.getCenter().getY();float r = circle.getRadius();float x1= (float) (-(Math.sqrt((k*k+1)*r*r-c*c*k*k+(2*c*d+2*b*c)*k-d*d-2*b*d-b*b)+(d+b)*k+c)/ (k*k+1));float y1 = (float) (-(k*(Math.sqrt(k*k*r*r+r*r-c*c*k*k+2*c*d*k+2*b*c*k-d*d-2*b*d-b*b)+c)+d*k*k-b)/ (k*k+1));float x2= (float) ((Math.sqrt((k*k+1)*r*r-c*c*k*k+(2*c*d+2*b*c)*k-d*d-2*b*d-b*b)-(d+b)*k-c)/ (k*k+1));float y2 = (float) (-(k*(-Math.sqrt(k*k*r*r+r*r-c*c*k*k+2*c*d*k+2*b*c*k-d*d-2*b*d-b*b)+c)+d*k*k-b)/ (k*k+1));points = new Point[2];points[0] = new Point(x1, y1);points[1] = new Point(x2, y2);}else {// 直线与y轴平行float distCtr = distPoint(circle.getCenter());float h = (float) Math.sqrt(circle.getRadius()*circle.getRadius() - distCtr*distCtr);float x = -this.c / this.a;float yCtr = circle.getCenter().getY();float y1 = yCtr + h;float y2 = yCtr - h;points = new Point[2];points[0] = new Point(x, y1);points[1] = new Point(x, y2);}}return points;}public Point[] intersectionRec(Rec rec) {Point[] points = new Point[2];Line lineLeft = rec.getLine(1);Line lineRight = rec.getLine(2);Line lineTop = rec.getLine(3);Line lineBottom = rec.getLine(4);Point ptleft = intersectionLine(lineLeft);Point ptRight = intersectionLine(lineRight);Point ptTop = intersectionLine(lineTop);Point ptBottom = intersectionLine(lineBottom);int i = 0;if (ptleft.getY() >= rec.getPoint2().getY() && ptleft.getY() <= rec.getPoint1().getY()) {points[i++] = ptleft;}if (ptRight.getY() >= rec.getPoint2().getY() && ptRight.getY() <= rec.getPoint1().getY()) {points[i++] = ptRight;}if(ptTop.getX() >= rec.getPoint1().getX() && ptTop.getX() <= rec.getPoint2().getX()) {points[i++] = ptTop;}if(ptBottom.getX() >= rec.getPoint1().getX() && ptBottom.getX() <= rec.getPoint2().getX()) {points[i++] = ptBottom;}return points;}@Overridepublic String toString() {return "Line{ " +a + "x + " + b + "y + " + c + " = 0 }";}}public class Exp2 {public static void main(String[] args) {// 线-线 交点测试//        Line line1 = new Line(9, 5, 3);//        Line line2 = new Line(4, 0, 9);//        String string = line1.intersectionLine(line2).toString();//        System.out.println(line1.toString());//        System.out.println(line2.toString());//        System.out.println("-------------------------");//        System.out.println(string);// 线-园 交点测试//        Point center = new Point(0, 0);//        Circle circle = new Circle(center, 1);//        Line line = new Line(2, 0, -1);//        Point[] intersections = line.intersectionCircle(circle);//        System.out.println(line.toString());//        System.out.println(circle.toString());//        System.out.println("-------------------------");//        System.out.println(intersections[0].toString());//        System.out.println(intersections[1].toString());// 线-矩 形交点测试//        Point point = new Point(0, 5);//        Point point1 = new Point(5, 0);//        Rec rec = new Rec(point, point1);//        Line line = new Line(-1, 1, -1);//        Point[] intersections = line.intersectionRec(rec);//        System.out.println(rec.toString());//        System.out.println(line.toString());//        System.out.println("-------------------------");//        System.out.println(intersections[0].toString());//        System.out.println(intersections[1].toString());}}
展开全文
• 题目：用面向对象的思维设计相关类，从而实现直线与直线、直线与圆、直线与矩形的交点。 菜鸡的C++实现： 公式： 直线与直线：ax+bx+c=0; 一般公式：X = (c * otherline.b - b* otherline.c) /(b * otherline.a ...
题目：用面向对象的思维设计相关类，从而实现直线与直线、直线与圆、直线与矩形的交点。
菜鸡的C++实现：

公式：
直线与直线：ax+bx+c=0;
 一般公式：X = (c * otherline.b - b* otherline.c)
/(b * otherline.a - a *  otherline.b);

直线与圆：（x-x0)2 +(y-y0)2 =r2      此处用y=kx+b为直线k=-c/b，b=-c
c = -cirle.x0;d = -cirle.y0;
一般公式：	x1=-(sqrt((k * k + 1) * cirle.r * cirle.r - c * c * k * k + (2 * c * d + 2 * b * c) * k - d * d - 2 * b * d - b * b) + (b + d) * k + c)
/ (k * k + 1);
x2= (sqrt((k * k + 1) * cirle.r * cirle.r - c * c * k * k + (2 * c * d + 2 * b * c) * k - d * d - 2 * b * d - b * b) - (b + d) * k - c)
/ (k * k + 1);

直线与矩形：考虑直线与四条直线在范围内是否有解即可
以下代码初步测试可用，若有bug谢谢指出
结构定义：graph.h
//圆
class Circle
{
public:
float r;
float x0;
float y0;
Circle(float r, float x0, float y0) {
this->r = r;
this->x0 = x0;
this->y0 = y0;

}
};
//矩形
class Rectangle {
public:
float topRightY;
float topRightx;
float bottomLeftY;
float bottomLeftX;
Rectangle(float topry, float toprx, float bottomly, float bottomlx) {
this->topRightx = toprx;
this->topRightY = topry;
this->bottomLeftX = bottomly;
this->bottomLeftY = bottomlx;
}

};
//直线
class Straight
{
public:

float a;
float b;
float c;
Straight(float a, float b, float c) {
this->a = a;
this->b = b;
this->c = c;

}
//求交点
void intersect(Straight otherline);
void intersect(Circle cirle);
void intersect(Rectangle rectangle);

};

算法实现graph.cpp
void Straight::intersect(Straight otherline)
{
if (this->b * otherline.a - this->a * otherline.b == 0)
{
if (this->c == otherline.c)
{
cout << "两直线重合，有无数交点" << endl;
return;
}
else
{
cout << "两直线平行无交点" << endl;
return;
}
}
else
{
float intersectX = (this->c * otherline.b - this->b * otherline.c) /
(this->b * otherline.a - this->a * otherline.b);
float intersectY= (this->a*otherline.c-this->c*otherline.a)/
(this->b * otherline.a - this->a * otherline.b);
cout << "两直线交点为：" << fixed << setprecision(2) << "(" <<
intersectX << "，" <<intersectY<< ")"<<endl;
}
}

void Straight::intersect(Circle cirle)
{
float distanceR;
distanceR = abs(this->a * cirle.x0 + this->b * cirle.y0 + this->c) /
(sqrt(this->a * this->a + this->b * this->b));
cout << "直线与圆心距离：" << distanceR << endl;
if (distanceR > cirle.r)
cout << "该直线与圆无交点" << endl;
else if (this->a == 0) {
float x1, x2;
float y1, y2;
x1 = cirle.x0 + sqrt(cirle.r* cirle.r-(cirle.y0-this->c/this->b)*(cirle.y0 - this->c / this->b));
x2 = cirle.x0 -sqrt(cirle.r * cirle.r - (cirle.y0 - this->c / this->b) * (cirle.y0 - this->c / this->b));
y1 = y2 = -this->c / this->b;
if (x1 == x2)
cout << "直线与圆有一交点：(" << x1 << "," << y1 << ")" << endl;
else
cout << "直线与圆有两交点：(" << x1 << "," << y1 << ")" << ",(" << x2 << ", " << y2 << ")" << endl;
}
else if (this->b == 0) {
float x1, x2;
float y1, y2;
y1 = cirle.y0 + sqrt(cirle.r * cirle.r - (cirle.-x0 - this->c / this->a) * (cirle.x0 - this->c / this->a));
y2 = cirle.y0 - sqrt(cirle.r * cirle.r - (cirle.x0 - this->c / this->a) * (cirle.x0 - this->c / this->a));
x1 = x2 = -this->c / this->a;
if (y1 ==y2)
cout << "直线与圆有一交点：(" << x1 << "," << y1 << ")" << endl;
else
cout << "直线与圆有两交点：(" << x1 << "," << y1 << ")" << ",(" << x2 << ", " << y2 << ")" << endl;
}
else
{
float x1,x2;
float y1,y2;
float k = -this->a / this->b;
float b = -this->c / this->b;
float c = -cirle.x0;
float d = -cirle.y0;
x1=-(sqrt((k * k + 1) * cirle.r * cirle.r - c * c * k * k + (2 * c * d + 2 * b * c) * k - d * d - 2 * b * d - b * b) + (b + d) * k + c)
/ (k * k + 1);
x2= (sqrt((k * k + 1) * cirle.r * cirle.r - c * c * k * k + (2 * c * d + 2 * b * c) * k - d * d - 2 * b * d - b * b) - (b + d) * k - c)
/ (k * k + 1);
y1 = k*x1 + b;
y2 = k * x2 + b;
if (x1 == x2)
cout << "直线与圆有一交点：(" << x1 << "," << y1 << ")" << endl;
else
cout << "直线与圆有两交点：(" << x1 << "," << y1 << ")"<< ",(" << x2 << ", " << y2 << ")" << endl;

}

}

void Straight::intersect(Rectangle rectangle)
{
if (this->a == 0) {
if ((-this->c / this->b) == rectangle.bottomLeftY || (-this->c / this->b) == rectangle.topRightY)
{
cout << "直线与矩形一边重合，有无数交点" << endl;
return;
}
else if ((-this->c / this->b) < rectangle.bottomLeftY || (-this->c / this->b) > rectangle.topRightY)
{
cout << "直线与矩形无交点<<endl";
return;
}
else
{
cout << "直线与矩形两交点：（"<<rectangle.bottomLeftX<<","<< (-this->c / this->b)<<"),(" << rectangle.topRightx << "," << (-this->c / this->b) << ")"<<endl;
return;
}
}
else if (this->b == 0) {
if ((-this->c / this->a) == rectangle.bottomLeftX || (-this->c / this->a) == rectangle.topRightx)
{
cout << "直线与矩形一边重合，有无数交点" << endl;
return;
}
else if ((-this->c / this->a) < rectangle.bottomLeftX || (-this->c / this->a) > rectangle.topRightx)
{
cout << "直线与矩形无交点<<endl";
return;
}
else
{
cout << "直线与矩形两交点：（" << -this->c / this->a << "," << rectangle.bottomLeftY << "),(" << -this->c / this->a << "," << rectangle.topRightY << ")" << endl;
return;
}
}
float X1, X2, X3, X4;
float Y1, Y2, Y3, Y4;
bool isHvae = false;
X1 = rectangle.bottomLeftX;
Y1 = -(this->a * X1 + this->c) / this->b;
X2 = rectangle.topRightx;
Y2 = -(this->a * X2 + this->c) / this->b;
Y3 = rectangle.bottomLeftY;
X3 = -(this->b * Y3 + this->c) / this->a;
Y4 = rectangle.topRightY;
X4 = -(this->b * Y4 + this->c) / this->a;
if (Y1 <= rectangle.topRightY && Y1 >= rectangle.bottomLeftY)
{
cout << "存在交点：（" <<X1 <<","<<Y1<<")";
isHvae = true;
}
if (Y2 <= rectangle.topRightY && Y2 >= rectangle.bottomLeftY)
{
cout << "存在交点：（" << X2 << "," << Y2 << ")";
isHvae = true;
}
if (X3 < rectangle.topRightx && X3 > rectangle.bottomLeftX)
{
cout << "存在交点：（" << X3 << "," << Y3 << ")";
isHvae = true;
}if (X4 < rectangle.topRightx && X4 > rectangle.bottomLeftX)
{
cout << "存在交点：（" << X4 << "," << Y4 << ")";
isHvae = true;
}
if(!isHvae)
cout << "无交点" << endl;
cout << endl;

}


main
int main() {
Straight straight(1, 1, -1);
Straight otherStraight(-1, 1, 0);
Rectangle rectangle(2, 2,1,-1);
Circle circle(0.5, 0.5, 0.5);
cout << "直线为：" << straight.a << "x+" << straight.b << "y+" << straight.c <<"=0;"
<< "圆为x0="<<circle.x0<<",y0="<<circle.y0<<",r="<<circle.r
<<"矩形：topRight=("<<rectangle.topRightx<<","<<rectangle.topRightY<<")bottomleft=("<<rectangle.bottomLeftX << "," << rectangle.bottomLeftY << ")"<<endl;
cout<<"直线与直线：" << endl;
straight.intersect(otherStraight);
cout << "直线与矩形：" << endl;
straight.intersect(rectangle);
cout << "直线与圆：" << endl;
straight.intersect(circle);

}



展开全文
• 用面向对象的思维设计相关类，从而实现直线与直线、直线与圆、直线与矩形的交点。 基本思想： 直线与直线，直线与圆：利用公式直接求解 直线与矩形：分解成直线与直线的交点： #include <iostream> #...
用面向对象的思维设计相关类，从而实现直线与直线、直线与圆、直线与矩形的交点。
基本思想：
直线与直线，直线与圆：利用公式直接求解
直线与矩形：分解成直线与直线的交点：


#include <iostream>

#include <math.h>

class VecPosition//坐标

{

public:

double mx;

double my;

public:

void setpoint(double x,double y)

{

mx = x;

my = y;

}

};

class Line直线

{

public:

double a;

double b;//ax+by+c=0

double c;

public:

void setLine(double ma,double mb,double mc)

{

a=ma;

b=mb;

c=mc;

}

};

class Circle圆

{

public:

VecPosition
center;

double R;

public :

void setcircle(VecPosition my_center,
double my_R)

{

center = my_center;

R = my_R;

}

};

class Rectangel矩形

{

public:

VecPosition top;

VecPosition bottem;

public:

void setRectangel(VecPosition
mytop,VecPosition mybottom)

{

top=mytop;

bottem=mybottom;

}

};

using namespace std;

直线与直线的交点//

VecPosition
LinecrossLine(Line l1,Line l2)

{

VecPosition cross;

double k1=(-1)*l1.a/l1.b;

double k2=(-1)*l2.a/l2.b;

double d;

if(k1==k2)

{

cout<<"没有交点"<<endl;

return cross;

}

else

{

d=l1.a*l2.b - l2.a*l1.b;

cross.mx = (l1.b*l2.c-l2.b*l1.c)/d;

cross.my = (l1.c*l2.a-l2.c*l1.a)/d;

return cross;

}

}

直线与圆的交点//

void LinecrossCircle(Line
l,Circle c)

{

double k;

double b;

double dc;//直线与与圆心的距离

double x1,y1,x2,y2;

double cx=c.center.mx;//cx,cy为圆心坐标；

double cy=c.center.my;

double r=c.R;//圆的半径；

if(l.b!=0)

{

k=(-1)*l.a/l.b;//转换为y=kx+b;

b=(-1)*l.c/l.b;

}

else

{

k=0;

b=(-1)*l.c/l.a;

}

dc =
(fabs(l.a*c.center.mx+l.b*c.center.my+l.c))/sqrt(l.a*l.a+l.b*l.b);//求直线与圆心的距离

double
f=sqrt((k*k+1)*r*r-cx*cx*k*k+2*(cx*cy+b*cx)*k-cy*cy-2*b*cy-b*b);//辅助运算

if(l.b!=0)

{

if(dc>c.R)

{

cout<<"圆与直线没有交点"<<endl;

}

else if(dc==c.R)

{

x1=((-1)*f+(cy+b)*k+cx)/(k*k+1);

y1=(-1)*(k*(f+cx)+cy*k*k-b)/(k*k+1);

cout<<"圆与直线只有一个交点，交点x="<<x1<<",y="<<y1<<endl;

}

else

{

x1=((-1)*f+(cy+b)*k+cx)/(k*k+1);

y1=k*x1+b;

x2=(f+(-1)*(cy+b)*k-cx)/(k*k+1);

y2=k*x2+b;

cout<<"第一个交点,("<<x1<<","<<y1<<")"<<endl;

cout<<"第二个交点,("<<x2<<","<<y2<<")"<<endl;

}

}

else// x=-c/a的情况；

{

if(dc>c.R)

{

cout<<"圆与直线没有交点"<<endl;

}

else if(dc==c.R)

{

x1=((-1)*f+(cy+b)*k+cx)/(k*k+1);

y1=(-1)*(k*(f+cx)+cy*k*k-b)/(k*k+1);

cout<<"圆与直线只有一个交点，交点x="<<y1<<",y="<<x1<<endl;

}

else

{

x1=((-1)*f+(cy+b)*k+cx)/(k*k+1);

y1=(-1)*(k*(f+cx)+cy*k*k-b)/(k*k+1);

cout<<"第一个交点,("<<y1<<","<<x1<<")"<<endl;

cout<<"第二个交点,("<<y1<<","<<(-1)*x1<<")"<<endl;

}

}

}

直线与矩形的交点//

Line getLine(VecPosition
top,VecPosition bottom)//由两个点获得直线

{

Line lTemp;

lTemp.a = top.my - bottom.my;

lTemp.b = bottom.mx- top.mx;

lTemp.c = top.mx*bottom.my -
bottom.mx*top.my;

return lTemp;

}

void LinecrossRectangel(Rectangel
rect,Line l0)

{

VecPosition x1,x2,x3,x4;

VecPosition cross1,cross2,cross3,cross4;

Line l1,l2,l3,l4;

x1.setpoint(rect.top.mx,rect.bottem.my);

x2.setpoint(rect.bottem.mx,rect.top.my);

x3=rect.top;

x4=rect.bottem;

l1=getLine(x4,x2);

l2=getLine(x4,x1);

l3=getLine(x2,x3);

l4=getLine(x1,x3);

cross1 = LinecrossLine(l1,l0);

cross2 = LinecrossLine(l2,l0);

cross3 = LinecrossLine(l3,l0);

cross4 = LinecrossLine(l4,l0);

int flag=0;

if(cross1.mx>=rect.bottem.mx&&cross1.mx<=rect.top.mx&&cross1.my>=rect.bottem.my&&cross1.my<=rect.top.my)

{

flag=1;

cout<<"交点,("<<cross1.mx<<","<<cross1.my<<")"<<endl;

}

if(cross2.mx>=rect.bottem.mx&&cross2.mx<=rect.top.mx&&cross2.my>=rect.bottem.my&&cross2.my<=rect.top.my)

{

flag=1;

cout<<"交点,("<<cross2.mx<<","<<cross2.my<<")"<<endl;

}

if(cross3.mx>=rect.bottem.mx&&cross3.mx<=rect.top.mx&&cross3.my>=rect.bottem.my&&cross3.my<=rect.top.my)

{

flag=1;

cout<<"交点,("<<cross3.mx<<","<<cross3.my<<")"<<endl;

}

if(cross4.mx>=rect.bottem.mx&&cross4.mx<=rect.top.mx&&cross4.my>=rect.bottem.my&&cross4.my<=rect.top.my)

{

flag=1;

cout<<"交点,("<<cross4.mx<<","<<cross4.my<<")"<<endl;

}

if(flag==0)

{

cout<<"直线与矩形没有交点"<<endl;

}

}

int main()

{

int position;

double a,b,c;

Line l1;

Line l2;

double R;

double cx,cy;

Circle c1;

VecPosition center;

VecPosition top,bottom;

Rectangel rect;

cout<<"直线与直线的交点"<<endl;

cout<<"直线与圆的交点"<<endl;

cout<<"直线与矩形的交点"<<endl;

cout<<"请输入请求："<<endl;

cin>>position;

switch(position)

{

case 1:

VecPosition cross;

cout<<"请输入第一条线的a,b,c"<<endl;

cin>>a>>b>>c;

l1.setLine(a,b,c);

cout<<"请输入第二条线的a,b,c"<<endl;

cin>>a>>b>>c;

l2.setLine(a,b,c);

cross=LinecrossLine(l1,l2);

cout<<"交点：x="<<cross.mx<<",y="<<cross.my;

break;

case 2:

cout<<"请输入直线的a,b,c"<<endl;

cin>>a>>b>>c;

l1.setLine(a,b,c);

cout<<"请分别输入圆心的坐标，圆的半径想x,y,R"<<endl;

cin>>cx>>cy>>R;

center.mx=cx;

center.my=cy;

c1.setcircle(center,R);

LinecrossCircle(l1,c1);

break;

case
3:

cout<<"请输入直线的a,b,c"<<endl;

cin>>a>>b>>c;

l1.setLine(a,b,c);

cout<<"请输入矩形右上角顶点的坐标"<<endl;

cin>>top.mx>>top.my;

cout<<"请输入矩形左下角顶点的坐标"<<endl;

cin>>bottom.mx>>bottom.my;

rect.setRectangel(top,bottom);

LinecrossRectangel(rect,l1);

break;

}

}




展开全文
• 1.求直线与圆的交点 (1)由线段端点P1（x1,y1）P2(x2,y2)得到线段所在直线的方程    ax+by+c=0   (2)由圆心P0(x0,y0)和半径r得到圆的方程    （x-x0）2+(y-y0)2=r2   (3)由点到圆的距离公式算出圆心...
1.求直线段与圆的交点

(1)由线段端点P1（x1,y1）P2(x2,y2)得到线段所在直线的方程

ax+by+c=0

(2)由圆心P0(x0,y0)和半径r得到圆的方程

（x-x0）2+(y-y0)2=r2

(3)由点到圆的距离公式算出圆心到线段的距离d

(4)我先假设圆与线段有两个公共点，将线段P1P2写成参数方程形式，t为参数。

x=x1+(x2-x1)*t

y=y1+(y2-y1)*t

0<=t<=1

这里我分别持有两个公共点类的对象，含有属性x,y和t。x与y是公共点坐标信息，参数t用来表示该点即使为直线与圆公共点是否不在线段范围内。

(5)若d>r则没有公共点，我们将两个公共点对象的参数t置为不合法，这样讲不被记录为多边形与圆的公共点。

(6)若d<=r则通过圆方程与线段参数方程求解公共点坐标与参数，将参数t合法的公共点对象记录为多边形与圆的公共点。这里对于d=r的情况视为有两个相同的公共点输出。

实现表现为项目中的SegCrossoverCircle函数

void SegCrossoverCircle(MyLine line, MyCircle circle, CrossoverPoint &cp1, CrossoverPoint &cp2)
{
//初始化图像信息
double x1, y1, x2, y2, x0, y0, r;
x1 = line.startpoint.x;
y1 = line.startpoint.y;
x2 = line.endpoint.x;
y2 = line.endpoint.y;
x0 = circle.center.x;
y0 = circle.center.y;

//寻找交点解参数方程
double a, b, c, a1, b1, c1, d, delta, t1, t2;
a = y0 - y1;
b = x1 - x0;
c = x0*y1 - x1*y0;
d = abs(a*x0 + b*y0 + c) / sqrt(a*a + b*b + esp);
a1 = x1*x1 + x2*x2 + y1*y1 + y2*y2 - 2 * x1*x2 - 2 * y1*y2;
b1 = 2 * (x0*x1 + x1*x2 + y0*y1 + y1*y2 - x0*x2 - y0*y2 - x1*x1 - y1*y1);
c1 = x0*x0 + x1*x1 + y0*y0 + y1*y1 - 2 * x0*x1 - 2 * y0*y1 - r*r;
delta = b1*b1 - 4 * a1*c1;

//出交点
if (d <= r)
{
if (delta < esp)
{
//相切只出一个
double cos1, sin1;
cp1.t = 2;
t1 = (-b1 + sqrt(delta)) / (2 * a1);
cp1.t = t1;
cp1.x = x1 + t1*(x2 - x1);
cp1.y = y1 + t1*(y2 - y1);
cos1 = (cp1.x - x0) / r;
sin1 = (cp1.y - y0) / r;
if (abs(sin(acos(cos1)) - sin1) < esp)
{
cp1.a = acos(cos1);
}
else
{
cp1.a = 2 * PI - acos(cos1);
}

//让pt2报废不算
cp2.t = 2;

}
else
{
double cos1, sin1, cos2, sin2;
t1 = (-b1 + sqrt(delta)) / (2 * a1);
t2 = (-b1 - sqrt(delta)) / (2 * a1);
cp1.t = t1;
cp1.x = x1 + t1*(x2 - x1);
cp1.y = y1 + t1*(y2 - y1);
cos1 = (cp1.x - x0) / r;
sin1 = (cp1.y - y0) / r;
if (abs(sin(acos(cos1)) - sin1) < esp)
{
cp1.a = acos(cos1);
}
else
{
cp1.a = 2 * PI - acos(cos1);
}

cp2.t = t2;
cp2.x = x1 + t2*(x2 - x1);
cp2.y = y1 + t2*(y2 - y1);
cos2 = (cp2.x - x0) / r;
sin2 = (cp2.y - y0) / r;
if (abs(sin(acos(cos2)) - sin2) < esp)
{
cp2.a = acos(cos2);
}
else
{
cp2.a = 2 * PI - acos(cos2);
}

}
}
else
{
//没交点让pt1,pt2都报废
cp1.t = 2;
cp2.t = 2;
}

}



展开全文
• 直线方程与圆方程联立求方程组解即可，使用时直接套用下面函数中公式即可，（x1,y1）（x2,y2）即为所求！ //下面这个函数得到交点，如果有2个就返回第一个求出。 CPoint GetPoint(int cx,int cy,int r,int...
• 使用C++求直线与直线、圆、矩形交点坐标几何类的编写圆矩形直线总结 几何类的编写 圆 没什么特别的，利用了圆的标准公式来表示圆 class Circle { private: double x_center_param; /* 圆心的 x 坐标值 */ double y...
• 一、直线1、直线倾斜角在平面直角坐标系中，当直线与x轴重合或平行时，规定倾斜角为0，对于与x轴相交直线，把x轴绕着交点按逆时针方向转到和直线重合时所转最小正角叫做直线倾斜角，倾斜角范围[0,π)。...
• 里面大部分计算 圆柱的交点类似，可以看下圆柱交点的推导过程： https://mp.csdn.net/postedit/92795146 点到直线的距离参考： https://mp.csdn.net/postedit/91954743 通过上面的公式求得k1,k2分别对于圆锥...
• 最近托哥的高三崽子们复习到了《直线与圆的位置关系》，有些童鞋对相切的理解有点混乱。因此，我专门写了这篇文章简单说明一下。两个问题两曲线相切一定只有一个交点吗？两曲线只有一个交点一定相切吗？回答这两个...
• 图1 极射投影原理示意图图2 两条过球心的直线与的交点全球极射投影极射投影的主要性质如下：在球面上平行于投影面的大，其极射投影是一个(圆心是极射投影中心)，称其基。在北半球球面上平行于投影面的小，...
• 目录C#常用数学计算公式方法01、求直线与的交点02、求直线与圆弧交点03、判断三点是否共线-即三角形面积为004、判断两条直线是否相互垂直05、与直线的交点06、判断直线是否与当前相交07、求圆弧长度07、获取...
• 圆的基本运算 1 .点是否在圆内 20 2 .求不共线的三点所确定的圆 21 ㈤ 矩形的基本运算 1.已知矩形三点坐标，求第4点坐标 22 ㈥ 常用算法的描述 22 ㈦ 补充 1．两圆关系： 24 2．判断圆是否在矩形内： 24 3．点...
• 2·3 通过圆与圆或圆与直线交点的圆 3.二次曲线 3·1 抛物线·椭圆·双曲线方程 3·2 二次曲线与直线 4.坐标变换 4·1 曲线移动 4·2 坐标轴平移 4·3 坐标轴旋转 4·4 一般二次曲线及二次曲线分类 4...
• 2·3 通过圆与圆或圆与直线交点的圆 3.二次曲线 3·1 抛物线·椭圆·双曲线方程 3·2 二次曲线与直线 4.坐标变换 4·1 曲线移动 4·2 坐标轴平移 4·3 坐标轴旋转 4·4 一般二次曲线及二次曲线分类 4...
• 5.5计算直线与圆的交点 20 5.6计算两个圆的交点 20 6. 球面 21 6.0给出地球经度纬度，计算圆心角 21 6.1已知经纬度，计算地球上两点直线距离 21 6.2已知经纬度，计算地球上两点球面距离 21 7. 三维几何的若干模板 22...
• LED临界导通状态下电压称为阈值电压，根据图7中拟合公式算出拟合直线与横轴交点 得到5种LED正常电压，为后续测试正常发光条件做准备。根据公式λ=[（hc）/e]Ud和上述数据，计算发光二极管发光波长与理论主...
• 1.高斯-克吕格投影高斯-克吕格投影这个投影是...在投影面上，中央子午线和赤道的投影都是直线，并且以中央子午线和赤道的交点0作为坐标原点，以中央子午线的投影为纵坐标轴，以赤道的投影为横坐标轴，这样便形成了高...
• 判断多边形的核是否存在 19 <br>㈣ 圆的基本运算 1 .点是否在圆内 20 2 .求不共线的三点所确定的圆 21 <br>㈤ 矩形的基本运算 1.已知矩形三点坐标，求第4点坐标 22 <br>㈥ 常用算法的描述 ...
• CSP202009-4星际旅行题目算法思想代码详解预处理2、最短距离的计算3、...两点之间的直线距离通过距离公式即可直接求出，而中心圆的部分则需要相关数学几何的知识即可求出。总体来看，这道题难度不大，大家可以尝试一下
• ★二次抛物线的顶点式一般式的互化及求二次抛物线Ｘ轴的交点。★特大非负整数的加、减和乘法运算，所得的都是精确值，速度极快！★求排列数和组合数。★将一个数分解质因数。★将一个数的平方根化为最简二次根式...
• ## 计算几何算法源码

热门讨论 2013-07-29 19:27:02
圆的基本运算 1 .点是否在圆内 2 .求不共线的三点所确定的圆 ㈤ 矩形的基本运算 1.已知矩形三点坐标，求第4点坐标 ㈥ 常用算法的描述 ㈦ 补充 1．两圆关系 2．判断圆是否在矩形内 3．点到平面的距离 4．点是否...
• 求线段或直线与圆的交点 28. 凸包的概念 29. 凸包的求法 ㈠ 点的基本运算 <br>1. 平面上两点之间距离 1 2. 判断两点是否重合 1 3. 矢量叉乘 1 4. 矢量点乘 2 5. 判断点是否在线段上 2 6...
• 求线段或直线与圆的交点 28. 凸包的概念 29. 凸包的求法 ㈠ 点的基本运算 <br>1. 平面上两点之间距离 1 2. 判断两点是否重合 1 3. 矢量叉乘 1 4. 矢量点乘 2 5. 判断点是否在线段上 2...
• 圆的基本运算 1 .点是否在圆内 20 2 .求不共线的三点所确定的圆 21 ㈤ 矩形的基本运算 1.已知矩形三点坐标，求第4点坐标 22 ㈥ 常用算法的描述 22 ㈦ 补充 1．两圆关系： 24 2．判断圆是否在矩形内...
• ## 计算几何

热门讨论 2012-08-06 21:49:17
圆的基本运算 1 .点是否在圆内 20 2 .求不共线的三点所确定的圆 21 ㈤ 矩形的基本运算 1.已知矩形三点坐标，求第4点坐标 22 ㈥ 常用算法的描述 22 ㈦ 补充 1．两圆关系： 24 2．判断圆是否在矩形内： ...
• 圆的基本运算 1 .点是否在圆内 20 2 .求不共线的三点所确定的圆 21 ㈤ 矩形的基本运算 1.已知矩形三点坐标，求第4点坐标 22 ㈥ 常用算法的描述 22 ㈦ 补充 1．两圆关系： 24 2．判断圆是否在矩形内： ...
• 圆的基本运算 1 .点是否在圆内 20 2 .求不共线的三点所确定的圆 21 ㈤ 矩形的基本运算 1.已知矩形三点坐标，求第4点坐标 22 ㈥ 常用算法的描述 22 ㈦ 补充 1．两圆关系： 24 2．判断圆是否在矩形内： ...
• ## 地理信息系统算法基础

千次下载 热门讨论 2009-06-20 10:57:53
2．21求线段或直线与圆的交点 2．22中心点的计算 2．23过点作垂线 2．24作平行线 2．25过点作平行线 2．26线段延长 2．27三点画圆 2．28线段打断 2．29前方交会 2．30距离交会 2．31极坐标作点 思考题 第3章空间数据...
• 1、该程序适用于计算器 CASVO fx-5800P，可计算线路中心成任意夹角缓和曲线、曲线、直线段中、边桩坐标及切线方位角。 2、A？ 输入转角：左转为负，右转为正 3、R？ 输入曲线半径 4、LS？ 输入缓和曲线长度 ...
• 2．21求线段或直线与圆的交点 2．22中心点的计算 2．23过点作垂线 2．24作平行线 2．25过点作平行线 2．26线段延长 2．27三点画圆 2．28线段打断 2．29前方交会 2．30距离交会 2．31极坐标作点 思考题 第3章空间数据...