博客
关于我
代码绘制五角形
阅读量:571 次
发布时间:2019-03-10

本文共 3375 字,大约阅读时间需要 11 分钟。

  • turtle绘制
    在Python中使用turtle库绘制五角星。需要注意的是,turtle的坐标系原点在窗口中心,x轴的正方向向右,y轴的正方向向上。以下是代码示例:
  • import turtleturtle.color('red')def get_points(x, y, length):    return [          (x, y),         (x + length, y),         (x, y - length),         (x + length/2, y + length/2),         (x + length, y + length)      ]def main():    for p in get_points(0, 0, 100):        turtle.goto(p[0], p[1])    turtle.goto(0, 0)main()turtle.done()

    可以通过上述代码绘制出如图所示的五角星图形。

    1. easyX绘制
      在C++中使用easyX库绘制五角星。此处需要注意的是,easyX的图形坐标系原点位于窗口的左上角,x轴的正方向向右,y轴的正方向向下。以下是代码示例:
    2. #include 
      #include
      #include
      namespace zsl { class Point { private: int x; int y; public: Point(int x, int y) { this->x = x; this->y = y; } Point to_offset(int dx, int dy) { return Point(this->x + dx, this->y + dy); } };}std::vector
      getPoints(zsl::Point p, int length) { std::vector
      pointList = { p, p.to_offset(length, 0), p.to_offset(0, length), p.to_offset(length/2, -length/2), p.to_offset(length, length), p }; return pointList;}int main() { initgraph(600, 600); setlinecolor(RED); zsl::Point start(200, 200); std::vector
      points = getPoints(start, 100); for (int i = 1; i < points.size(); i++) { zsl::Point lastPoint = points[i-1]; zsl::Point currentPoint = points[i]; line(lastPoint.x, lastPoint.y, currentPoint.x, currentPoint.y); } int a; std::cin >> a; closegraph(); return 0;}

      通过上述代码可以绘制出与示意图类似的五角星图形。

      1. Swing绘制
        在Java Swing框架中绘制五角星的代码示例如下:
      2. package com.demo.draws;import javax.swing.*;import java.awt.*;import java.util.Arrays;public class Draw extends JFrame {    public class Plot extends JPanel {        private List
        xCoordinates; private List
        yCoordinates; public Plot(List
        points) { this.points = points; } public void paint(Graphics graphics) { super.paint(graphics); Graphics2D g2d = (Graphics2D) graphics; g2d.setColor(Color.RED); for (int i = 1; i < points.size(); i++) { g2d.drawLine( points.get(i-1)[0], points.get(i-1)[1], points.get(i)[0], points.get(i)[1] ); } BasicStroke basicStroke = new BasicStroke(10.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL); g2d.setStroke(basicStroke); } } public List
        getPoints(int x, int y, int length) { return Arrays.asList( Arrays.asList(x, y), Arrays.asList(x+length, y), Arrays.asList(x, y+length), Arrays.asList(x+length/2, y-length/2), Arrays.asList(x+length, y+length), Arrays.asList(x, y) ); } public Draw() { super(); init(); } public void init() { this.setSize(600, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setContentPane(new Plot(getPoints(200, 200, 100))); this.setTitle("绘图"); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new Draw(); }}

        通过以上代码可以在swing组件中绘制出与示意图一致的五角星图形。

    转载地址:http://zzwvz.baihongyu.com/

    你可能感兴趣的文章
    nexus上传jar
    查看>>
    Nexus指南中的更新强调集成和透明度的重要性
    查看>>
    Nexus指南已经发布
    查看>>
    Nexus(1):Nexus的安装与配置
    查看>>
    NFinal学习笔记 02—NFinalBuild
    查看>>
    NFS
    查看>>
    NFS Server及Client配置与挂载详解
    查看>>
    NFS 服务配置篇
    查看>>
    NFS共享文件系统搭建
    查看>>
    nfs复习
    查看>>
    NFS安装配置
    查看>>
    NFS服务器配置-服务启动与停止
    查看>>
    NFS的安装以及windows/linux挂载linux网络文件系统NFS
    查看>>
    NFS的常用挂载参数
    查看>>
    NFS网络文件系统
    查看>>
    NFS远程目录挂载
    查看>>
    nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
    查看>>
    NFV商用可行新华三vBRAS方案实践验证
    查看>>
    ng build --aot --prod生成文件报错
    查看>>
    ng 指令的自定义、使用
    查看>>