<分区>
Line2D.cs
namespace ConsoleApp1
{
public class Line2D
{
public String name; // any name
public Point2d p1;
public Point2d p2;
public Line2D(Point2d p1, Point2d p2, String name)
{
this.name = name;
this.p1 = p1;
this.p2 = p2;
}
public Point2d nearPoint(Point2d p)
{
double slope = (this.p2.y - this.p1.y);
double coffX = slope;
double coffY = -1;
double const1= slope * this.p1.x - this.p1.y;
//AX + BY=C
//imaginary line perpendicular to line and passing through given point
double slope1 = -1 * slope;
double coffX1 = slope;
double coffY1 = -1;
double const2 = slope * this.p1.x - this.p1.y;
double X = (const1 - const2) / (coffX - coffX1);
double Y = coffX * X - const1;
String Name = "nearPoint";
Point2d P = new Point2d (X, Y,Name);
return p;
}
}
}
点二维.cs
namespace ConsoleApp1
{
public double x;
public double y;
public String name;
public Point2d(double x,double y,string name)
{
this.x = x;
this.y = y;
this.name = name;
}
}
}
程序.cs
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter X cordinate of point:");
double x = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("enter Y cordinate of point:");
double y = Convert.ToDouble(Console.ReadLine());
Point2d p = new Point2d(x,y,"nischal");
Console.WriteLine("enter 1st point:"); //Line12
Point2d p1 = Console.ReadLine();
Console.WriteLine("enter 2nd point:"); //Line 14
Point2d p2 = Console.ReadLine();
Line2D l = new Line2D(p1,p2,"nischal"); // Line 16
}
}
问题:我想创建一个 Line2D 对象并调用它的 nearPoint 方法。但我坚持第 12 行第 14 16 行。我希望用户输入数据类型的 p1 和 p2 的值,这些数据类型是 Point2d p1 和 Point2d p2 数据类型,这些数据类型在 Line2D 类上进行计算。但是我在第 12 行和第 14 行转换时出错。请高手帮我解决这个问题。它是 C# 控制台应用程序代码。