c#点线面(非继承)

1.把定义平面直角坐标系上的一个点的CPoint作为结构,描述一条直线的类Cline,再派生出一个矩形类CRect。要求成员函数能求出两点间的距离、矩形的周长和面积等。设计一个测试程序,并构造完整的程序。 提示:(1)可能用到的函数:math类中的函数 (2)注意数据类型的转换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public interface zhaojiaxiang
    {
        double area();
    }
    public class CPoint
    {
        private double x;
        private double y;
        public CPoint() { }
        public CPoint(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
        public double getx()
        {
            return x;
        }
        public double gety()
        {
            return y;
        }

    }
    public class Cline
    {
        private CPoint n1;
        private CPoint n2;
        public Cline() { }
        public Cline(CPoint n1,CPoint n2)
        {
            this.n1=n1;
            this.n2=n2;
        }
        public double lineLength()
        {
            return System.Math.Sqrt((n1.getx() - n2.getx()) * (n1.getx() - n2.getx()) + (n1.gety() - n2.gety())*(n1.gety() - n2.gety()));
        }
    }
    public class Cert:zhaojiaxiang 
    {
        private Cline n1;
        private Cline n2;
        public Cert() { }
        public Cert(Cline n1,Cline n2)
        {
            this.n1 = n1;
            this.n2 = n2;
 
        }
        public double area()
        {
            return n1.lineLength() * n2.lineLength();
        }
        public double zhouchang()
        {
            return (n1.lineLength() + n2.lineLength()) * 2;
        }
        
    }

    class Program
    {
        static void Main(string[] args)
        {
            CPoint h = new CPoint(3, 5);
            CPoint h1 = new CPoint(6, 5);
            CPoint h2 = new CPoint(5, 5);
            Cline w1 = new Cline(h, h1);
            Cline w2 = new Cline(h, h2);
            Cert n = new Cert(w1,w2);
            Console.WriteLine("周长是:{0}",n.zhouchang());                                                                                                                                                                                                                                                                                                          
            Console.Write("面积是:{0}",n.area());
            Console.Read();
        }
    }
}

### C# 中使用 Graphics 类绘制、线、C# 的 Windows Forms 应用程序中,`Graphics` 类用于执行绘图操作。通过 `Paint` 事件或重写的 `OnPaint` 方法可以访问 `Graphics` 对象并完成各种图形的绘制。 以下是完整的示例代码以及解释: #### 定义变量 首先,在窗体类中定义必要的成员变量: ```csharp public partial class Form1 : Form { Image myImage; // 存储图像对象 Graphics g; // 图形绘制对象 Pen pen = new Pen(Color.Black, 2); // 创建画笔对象 int PBheight, PBwidth; // 记录画布大小 int drawtype = 0; // 确定绘制模式 (0: , 1: 线, 2: ) public Form1() { InitializeComponent(); this.Paint += new PaintEventHandler(ShowPictureBox1_Paint); } } ``` #### 处理 Paint 事件 在 `Paint` 事件中初始化 `Graphics` 对象,并根据需求绘制不同的图形: ```csharp private void ShowPictureBox1_Paint(object sender, PaintEventArgs e) { g = e.Graphics; // 获取 Graphics 对象 PBheight = this.ClientSize.Height; PBwidth = this.ClientSize.Width; switch (drawtype) { case 0: DrawPoint(g, pen); // 绘制 break; case 1: DrawLine(g, pen); // 绘制线 break; case 2: DrawShape(g, pen); // 绘制 break; } } ``` #### 实现具体功能 下分别实现、线、的绘制逻辑: ##### 绘制单个 可以通过调用 `DrawEllipse` 或其他方法模拟的绘制效果: ```csharp void DrawPoint(Graphics g, Pen p) { int pointX = PBwidth / 2; // 设置的位置 int pointY = PBheight / 2; g.DrawEllipse(p, pointX - 1, pointY - 1, 2, 2); // 使用椭圆表示[^1] } ``` ##### 绘制直线 使用 `DrawLine` 方法指定起和终坐标即可: ```csharp void DrawLine(Graphics g, Pen p) { Point startPoint = new Point(PBwidth / 4, PBheight / 4); // 起始位置 Point endPoint = new Point(3 * PBwidth / 4, 3 * PBheight / 4); // 结束位置 g.DrawLine(p, startPoint, endPoint); // 连接两形成一条线[^2] } ``` ##### 绘制封闭形状(矩形作为例子) 利用 `FillRectangle` 方法填充颜色,或者仅勾勒边界: ```csharp void DrawShape(Graphics g, Pen p) { Rectangle rect = new Rectangle(PBwidth / 4, PBheight / 4, PBwidth / 2, PBheight / 2); using (Brush brush = new SolidBrush(Color.LightBlue)) // 填充颜色 { g.FillRectangle(brush, rect); // 填充矩形内部 } g.DrawRectangle(p, rect); // 可选:绘制矩形轮廓[^3] } ``` 以上代码展示了如何基于用户输入或其他条件动态切换不同类型的图形渲染方式。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值