package com.anyec.math; import java.awt.Color; import java.awt.Graphics; import java.util.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class LineDemo extends JFrame { private static int WIDHT = 1000, HEIGHT = 1000; private IPoint pa = new IPoint(WIDHT / 2, HEIGHT / 2); private Double rf = 50d, ra = 200d, re = 123.81; private IPoint pf = new IPoint(WIDHT / 2 + rf, HEIGHT / 2); private IPoint pb, pc, pd, pe, pe0; private Timer t = new Timer(); private Double minY, maxY, currY, step = 0.5; private ICircle ca, ce, cf; public LineDemo() { pe0 = new IPoint( (new Float(500 + 200 * Math.sin(Math.PI / 3)).intValue() + new Float(500 + 200 * Math.sin(Math.PI / 3)).intValue()) - (500 + 100), 500 - 200 / 2 + 500 + 200 / 2 - 500); ca = new ICircle(pa, ra); cf=new ICircle(pf, rf); minY=500-250d; maxY=500+250d; } public static void main(String[] args) { LineDemo demo = new LineDemo(); demo.setSize(WIDTH, HEIGHT); demo.resize(WIDTH, HEIGHT); demo.setVisible(true); demo.startTimer(); } public void startTimer() { t.schedule(new TimerTask() { @Override public void run() { SwingUtilities.invokeLater(new Runnable() { public void run() { runCal(); } }); } }, new Date(), 50); } public void runCal(){ if (currY == null) currY = minY; if (currY < minY || currY > maxY) step = -1 * step; pe = new IPoint(pe0.getX(), currY); ce = new ICircle(pe, re); List<IPoint> list = ICircle.pointSymmetry(ca, ce); if (list.size() < 2) { currY = currY + step; return; } pb = list.get(0); pc = list.get(1); pd = ILine.pointSymmetry(pb, pc, pe); currY = currY + step; this.update(this.getContentPane().getGraphics()); } @Override public void update(Graphics g) { super.update(g); clear(g); init2(g); update2(g); } public void update2(Graphics g) { g.setColor(Color.RED); if(pb!=null&&pc!=null){ ILine.draw2(g, pd, pc); ILine.draw2(g, pd, pb); ILine.draw2(g, pe, pc); ILine.draw2(g, pe, pb); pc.drawInfo(g, "C"); pe.drawInfo(g, "E"); pb.drawInfo(g, "B"); pd.drawInfo(g, "D"); } } public void clear(Graphics g){ g.setColor( getBackground() ); g.fillRect( 0, 0, getWidth(), getHeight() ); paint( g ); } public void init2(Graphics g){ g.setColor(Color.BLACK); g.drawLine(0, 500, 1000, 500); g.drawLine(500, 0, 500, 1000); g.drawLine(pe0.getX().intValue(), 0, pe0.getX().intValue(), 1000); pa.drawInfo(g, "A"); pf.drawInfo2(g, "F"); ca.draw(g); cf.draw(g); } @Override public void paint(Graphics g) { super.paint(g); init2(g); } } public class IPoint { static NumberFormat format=NumberFormat.getInstance(); private Double x; private Double y; public Double getX() { return x; } public void setX(double x) { this.x = x; } public Double getY() { return y; } public void setY(double y) { this.y = y; } public IPoint(double x, double y) { super(); this.x = x; this.y = y; } public IPoint() { super(); } public void drawInfo(Graphics g,String label){ format.setMaximumFractionDigits(2); g.drawArc(x.intValue(), y.intValue(), 2, 2, 0, 360); g.drawString(label+"("+format.format(x)+","+format.format(y)+")", x.intValue()-5, y.intValue()-5); } public void drawInfo2(Graphics g,String label){ g.drawArc(x.intValue(), y.intValue(), 2, 2, 0, 360); g.drawString(label+"("+format.format(x)+","+format.format(y)+")", x.intValue()+5, y.intValue()+5); } public static Double distance(IPoint p1,IPoint p2){ return Math.sqrt(Math.pow(p1.getX()-p2.getX(), 2)+Math.pow(p1.getY()-p2.getY(), 2)); } public void print(){ System.out.println("x:"+x+" y:"+y); } } package com.anyec.math; import java.awt.Graphics; import java.util.ArrayList; import java.util.List; public class ICircle { private Double x0; private Double y0; private Double r; public double getX0() { return x0; } public void setX0(double x0) { this.x0 = x0; } public double getY0() { return y0; } public void setY0(double y0) { this.y0 = y0; } public double getR() { return r; } public void setR(double r) { this.r = r; } public ICircle() { super(); } public ICircle(double x0, double y0, double r) { super(); this.x0 = x0; this.y0 = y0; this.r = r; } public ICircle(IPoint p, double r) { super(); this.x0 = p.getX(); this.y0 = p.getY(); this.r = r; } public IPoint getPoint(){ return new IPoint(x0,y0); } public static List<IPoint> pointSymmetry(ICircle c1,ICircle c2){ List<IPoint> list=new ArrayList<>(); double delt=0; double distance=IPoint.distance(c1.getPoint(), c2.getPoint()); if(distance>(c1.getR()+c2.getR())||distance<Math.abs((c1.getR()-c2.getR()))){ System.out.println("没有交点"); return list; } /*** * y=(x1-x2)*x/(y2-y1) + (r1^2-r2^2-x1^2+x2^2-y1^2+y2^2 )/(2y2-2y1) * k=(x1-x2)/(y2-y1) * c=(r1^2-r2^2-x1^2+x2^2-y1^2+y2^2 )/(2y2-2y1) * * y=kx+c * * 带入 园1 * * (x-x1)^2+(kx+c-y1)^2=r1^2 * * x^2-2*x1*x+x1^2+k^2*x^2 * * * * (1+k^2)*x^2+( 2k*(c-y1) -2*x1 ) *x + x1^2+(c-y1)^2-r1^2=0; * a=(1+k^2) * b= ( 2k*(c-y1) -2*x1 ) * c= x1^2+(c-y1)^2-r1^2 */ if(c1.getY0()!=c2.getY0()){ double k=(c1.getX0()-c2.getX0())/(c2.getY0()-c1.getY0()); double cc=(c1.getR()*c1.getR()-c2.getR()*c2.getR()-c1.getY0()*c1.getY0()-c1.getX0()*c1.getX0()+c2.getY0()*c2.getY0()+c2.getX0()*c2.getX0())/2/(c2.getY0()-c1.getY0()); double d=cc-c1.getY0(); double a=(1+k*k); double b=2*k*d-2*c1.getX0(); double c=c1.getX0()*c1.getX0()+d*d-c1.getR()*c1.getR(); //以上ok double x1=-1*(b+Math.sqrt(b*b-4*a*c))/2/a; double y1=k*x1+cc; double x2=-1*(b-Math.sqrt(b*b-4*a*c))/2/a; double y2=k*x2+cc; list.add(new IPoint(x1, y1)); if(x1!=x2&&y1!=y2){ list.add(new IPoint(x2, y2)); } }else if(c1.getX0()!=c2.getX0()){ double k=(c1.getY0()-c2.getY0())/(c2.getX0()-c1.getX0()); double cc=(c1.getR()*c1.getR()-c2.getR()*c2.getR()-c1.getY0()*c1.getY0()-c1.getX0()*c1.getX0()+c2.getY0()*c2.getY0()+c2.getX0()*c2.getX0())/2/(c2.getX0()-c1.getX0()); double d=cc-c1.getX0(); double a=(1+k*k); double b=2*k*d-2*c1.getY0(); double c=d*d+c1.getY0()*c1.getY0()-c1.getR()*c1.getR(); double y1=-1*(b+Math.sqrt(b*b-4*a*c))/2/a; double x1=k*y1+cc; double y2=-1*(b-Math.sqrt(b*b-4*a*c))/2/a; double x2=k*y2+cc; list.add(new IPoint(x1, y1)); if(x1!=x2&&y1!=y2){ list.add(new IPoint(x2, y2)); } } return list; } //求c相对直线a b的对称点 // public static List<IPoint> pointSymmetry2(ICircle c1,ICircle c2){ // List<IPoint> list=new ArrayList<>(); // // double x1=c1.getX0(),x2=c2.getX0(),y1=c1.getY0(),y2=c2.getY0(); // double r2=c1.getR(),r1=c2.getR(); // // // // double x_1 = 0,x_2=0,y_1=0,y_2=0; // // // // //如果 y1!=y2 // if(y1!=y2){ // // //为了方便代入 // double A = (x1*x1 - x2*x2 +y1*y1 - y2*y2 + r2*r2 - r1*r1)/(2*(y1-y2)); // double B = (x1-x2)/(y1-y2); // // double a = 1 + B * B; // double b = -2 * (x1 + (A-y1)*B); // double c = x1*x1 + (A-y1)*(A-y1) - r1*r1; // // //下面使用判定式 判断是否有解 // double delta=b*b-4*a*c; // // if(delta >0) // { // // x_1=(-b+Math.sqrt(b*b-4*a*c))/(2*a); // x_2=(-b-Math.sqrt(b*b-4*a*c))/(2*a); // y_1 = A - B*x_1; // y_2 = A - B*x_2; // list.add(new IPoint(x_1,y_1)); // list.add(new IPoint(x_2,y_2)); // } // else if(delta ==0) // { // x_1 = x_2 = -b/(2*a); // y_1 = y_2 = A - B*x_1; // list.add(new IPoint(x_1,y_1)); // }else // { // System.err.println("两个圆不相交"); // return null; // } // } // else if(x1!=x2){ // // //当y1=y2时,x的两个解相等 // x_1 = x_2 = (x1*x1 - x2*x2 + r2*r2 - r1*r1)/(2*(x1-x2)); // // double a = 1 ; // double b = -2*y1; // double c = y1*y1 - r1*r1 + (x_1-x1)*(x_1-x1); // // double delta=b*b-4*a*c; // // if(delta >0) // { // y_1 = (-b+Math.sqrt(b*b-4*a*c))/(2*a); // y_2 = (-b-Math.sqrt(b*b-4*a*c))/(2*a); // list.add(new IPoint(x_1,y_1)); // list.add(new IPoint(x_2,y_2)); // } // else if(delta ==0) // { // y_1=y_2=-b/(2*a); // list.add(new IPoint(x_1,y_1)); // }else // { // System.err.println("两个圆不相交"); // } // } // else // { // System.out.println("无解"); // // } // return list; // // } public void draw(Graphics g){ g.drawArc(new Double(x0-r).intValue(), new Double(y0-r).intValue(), r.intValue()*2, r.intValue()*2, 0, 360); } public static void main(String[] args) { List<IPoint> list=pointSymmetry(new ICircle(0, 0, 2),new ICircle(2,0,2)); for(IPoint p:list){ System.out.println(p.getX()+" "+p.getY()); } } } package com.anyec.math; import java.awt.Graphics; import java.text.NumberFormat; public class ILine { private double k = 1; private double c = 0; public ILine() { super(); } public ILine(double k, double c) { super(); this.k = k; this.c = c; } public Double getK() { return k; } public void setK(double k) { this.k = k; } public Double getC() { return c; } public void setC(double c) { this.c = c; } public static ILine getLine(IPoint p1, IPoint p2) { ILine line = new ILine(); line.setK((p1.getY() - p2.getY()) / (p1.getX() - p2.getX())); line.setC(p2.getY() - line.getK() * p2.getX()); return line; } public Double getY(Double x) { return k * x + c; } public Double getX(Double y) { return (y - c) / k; } static NumberFormat nf= NumberFormat.getInstance(); public static void draw(Graphics g,IPoint a, IPoint b){ g.drawLine(a.getX().intValue(), a.getY().intValue(), b.getX().intValue(), b.getY().intValue()); double len=Math.sqrt(Math.pow(a.getX()-b.getX(),2)+Math.pow(a.getY()-b.getY(),2)); g.drawString(nf.format(len), new Double((a.getX()+b.getX())/2).intValue(), new Double((a.getY()+b.getY())/2).intValue()); } public static void draw2(Graphics g,IPoint a, IPoint b){ g.drawLine(a.getX().intValue(), a.getY().intValue(), b.getX().intValue(), b.getY().intValue()); } // 求p相对直线a b的对称点 public static IPoint pointSymmetry(IPoint a, IPoint b, IPoint p) { if(a.getX()==b.getX()){ double x = 2*a.getX()-p.getX(); double y=p.getY(); return new IPoint(x, y); }else if(a.getY()==b.getY()){ double y = 2*a.getY()-p.getY(); double x=p.getX(); return new IPoint(x, y); }else{ ILine line = getLine(a, b); return pointSymmetry(line, p); } } // 求p相对直线y=kx+c的对称点 public static IPoint pointSymmetry(ILine line1, IPoint p) { // 计算经过p的直线方程的k c ILine line = new ILine(); line.setK(-1/line1.getK()); line.setC(p.getY()-line.getK()*p.getX()); // 计算中点垂直交点 Double x0=line1.getK()*(line.getC()-line1.getC())/(line1.getK()*line1.getK()+1); Double y0=line1.getK()*x0+line1.getC(); Double x=2*x0-p.getX(); Double y=2*y0-p.getY(); return new IPoint(x,y); } //点到直线的垂点 public static IPoint jd(ILine line1, IPoint p) { // 计算经过p的直线方程的k c ILine line = new ILine(); line.setK(-1/line1.getK()); line.setC(p.getY()-line.getK()*p.getX()); // 计算中点垂直交点 Double x0=line1.getK()*(line.getC()-line1.getC())/(line.getK()*line.getK()+1); Double y0=line1.getK()*line1.getK()*(line.getC()-line1.getC())/(line.getK()*line.getK()+1)+line1.getC(); return new IPoint(x0,y0); } public static void main(String[] args) { IPoint p= pointSymmetry(new ILine(1,0),new IPoint(0,1)); System.out.println(p.getX()+" "+p.getY()); p= pointSymmetry(new ILine(1,1),new IPoint(1,0)); System.out.println(p.getX()+" "+p.getY()); } }
相关推荐
Java图形界面时钟是一种利用Java编程语言开发的GUI(图形用户界面)应用程序,它能够模拟现实世界中的机械钟,展示当前的时间。在这个程序中,开发者通过设计和实现一个美观的界面,增强了用户的视觉体验,使其在...
在机械虚拟仿真领域,UG NX软件内置了Java虚拟机,支持Java的二次开发,采用这两种软件可以创设虚拟仿真环境和虚拟仿真界面,为农机虚拟漫游系统的创设提供了条件。 在农机设计时,现代化设计工厂一般都采用了数字...
本书编写过程中得到了各级领导和机械工业出版社华章分社的大力支持,在此表示衷心的感谢。 限于作者水平,书中难免有不足与疏漏之处,敬请专家、同行及广大读者批评指正! 为便于老师教学,我们将为选用本教材的...
通过Physics-Engine,开发者可以创建出各种有趣且逼真的物理场景,比如模拟桥梁的动态稳定性、研究机械装置的工作原理,甚至在游戏开发中实现复杂的物理交互。尽管Java不是传统的高性能计算语言,但其丰富的库支持和...
此外,还可以通过编程实现对FRC组件的控制逻辑,例如,使用PID控制器或其他算法来调整转向架的角度,确保火箭的直线飞行。 在编程方面,FRC组件通常与微控制器如Arduino或Raspberry Pi相连,通过C++或Java等语言...
Simulink是MATLAB环境下的一个模块化动态系统建模工具,它允许用户通过图形化界面构建、模拟和分析各种复杂系统,包括电气、机械、液压等。 首先,我们来看电液伺服系统的基本构成。它主要由电动机、液压泵、伺服阀...
通过镜头获取现实世界的物理影像,烙影可以让摄像头(或相机)完成识别和定位、检测表面缺陷、状态识别、尺寸测量等功能,是学习机器视觉技术、...支持c++, vb.net, c#, labview, java等多种主流开发语言和开发环境。
在没有外力作用下,球体会保持静止或匀速直线运动;当施加外力时,球体会加速或改变方向。 2. 运动方程:球体的运动可以用微分方程来描述,例如平抛运动(仅受重力影响)或自由落体(忽略空气阻力)。这些方程通常...
学生需要运用所学知识,通过多次试验找到合适的PWM调速模拟量,并结合电脑鼠电路图,掌握引脚连接情况,最终完成姿势修正子程序的编写。 这种教学模式的成功,说明职业教育领域的人工智能教育不仅能够培养学生的...
1. **直线竞速类**:最基础的竞赛形式,目标是让车辆在直线上尽可能快地通过终点,主要考验车辆的加速性能和稳定性。 2. **赛道导航类**:智能车需自动识别并沿着设定的赛道行驶,通常包含弯道,对路径规划和避障...
6. **模拟和调试**:在实际硬件测试之前,开发者会使用仿真工具(如RobotBuilder或Simulink)来验证运动控制算法。这有助于在物理系统上实施前发现潜在问题。 7. **框架和库**:在Java中,FRC团队通常使用WPILib,...
在"Computational Geometry: ITMO VM 2014-2015"这个主题中,我们可以推测这是俄罗斯圣彼得堡信息技术机械与光学大学(ITMO University)在2014至2015学年举办的一次虚拟机器(VM)比赛或者课程,旨在让学生掌握计算...