Java and matlab drawing polygon closure polyline diagram example

  • 2020-04-01 03:03:56
  • OfStack

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201402/20140227144312.jpg? 201412714466 ">

1. Use matlab for closed polygon diagram

No function was found to draw the polygon directly, except to save the coordinates of each point in an array, connect a point to its neighbor, and connect the last point to the first point. Here is a sample. M file:


clear;
clc;
a=[0 2 4 6 8 10 12 14;0 2 1 4 6 6 5 7];  % The coordinates of the points to connect  x;y
[n,m]=size(a);
for i=1:m-1;
    line([a(1,i),a(1,i+1)],[a(2,i),a(2,i+1)]);  % Connect the nodes line([x1,x2],[y1,y2])
    hold on
end
hold on
line([a(1,1),a(1,m)],[a(2,1),a(2,m)]);  % The head and tail nodes are connected 

  2. Use Java to make closed polygon diagrams

The Graphics class drawPolygon function in Java provides direct polygon plotting.

Function prototype: public abstract void drawPolygon(int[] xPoints, int[] yPoints,int nPoints)

instructions

(1) draws a closed polygon defined by an array of x and y coordinates. Each pair of (x, y) coordinates defines a point. NPoints is the number of line segments.

(2) the preceding npoint-1 line segment is the line segment from (xPoints[I -1], yPoints[I -1]) to (xPoints[I], yPoints[I]) when 1 Or less I Or less nPoints. If the last point is different from the first, the graph closes automatically by drawing a line segment between the two points.

The code example is as follows:


import java.applet.*;
import java.awt.*;
public class DrawPicture extends Applet 
{   public void paint(Graphics g)
    { 
      int px[]={20,70,130,240};
      int py[]={20,150,100,130};
      g.drawPolygon(px,py,4); 
    }      
}

3. The instructions

Whether a polygon can be formed, convex, or concave depends on the order of the points given.

The program is only responsible for connecting adjacent nodes, and connecting head to tail.


Related articles: