ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Javaの課題丸投げコミュの多角形の線を分割する方法がわかりません

  • mixiチェック
  • このエントリーをはてなブックマークに追加
課題では多角形の作成と点の削除、線を分割せいて新しい点を挿入できるようにするんですが線を分割して新しい点を挿入するほうほうがわかりません!
宜しくお願いします

public static void main(String[] args)
{
JFrame frame = new JFrame();


frame.setTitle( "課題4" );


frame.setSize( 800, 800 );


frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


MyPanel panel = new MyPanel();
JButton button = new JButton("Clear");
button.setPreferredSize(new Dimension(60, 20));
panel.add(button);
frame.getContentPane().add( panel );


frame.setVisible( true );
}
}




class MyPanel extends JPanel implements MouseListener
{
List<List<Point>> polygons = new ArrayList<>();

public MyPanel()
{
setBackground( Color.white );
addMouseListener((MouseListener)this);
}

@Override
public void mouseClicked(MouseEvent e)
{
Point p = e.getPoint();
boolean rightClick = e.getButton() == MouseEvent.BUTTON3;

if (polygons.isEmpty() || ((polygons.get(polygons.size() - 1)).size() >= 3 && rightClick)) {
polygons.add(new ArrayList<>());
}

List<Point> latestPoints = polygons.get(polygons.size() - 1);

// クリックした点がこれまでの多角形の内部にないかどうかチェック
for (List<Point> points : polygons) {
if (points.equals(latestPoints) || points.size() < 3) break;

Polygon polygon = new Polygon();
for (Point point : points) {
polygon.addPoint(point.x, point.y);
}
if (polygon.contains(p)) {
return;
}
}

// すでにある点をクリックしたら削除
for (Point point : latestPoints) {
if (distance(point, p) < 2.5) {
latestPoints.remove(point);
drawPolygons(rightClick);
return;
}
}

// クリックした点がこれまでの多角形と交差しないかどうかチェック
for (List<Point> points : polygons) {
if (points.equals(latestPoints)) break;

for (int i = 0; i < points.size(); i++) {
int ax = points.get(i).x;
int ay = points.get(i).y;
int bx = points.get((i + 1) % points.size()).x;
int by = points.get((i + 1) % points.size()).y;
for (int j = 0; j < latestPoints.size(); j++) {
int cx = latestPoints.get(j).x;
int cy = latestPoints.get(j).y;
if (isIntersect(ax, ay, bx, by, cx, cy, p.x, p.y)) return;
}
}
}

if (!rightClick) latestPoints.add(p);

drawPolygons(rightClick);
}

private double distance(Point a, Point b) {
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}

private void drawPolygons(boolean drawLine) {
clear();
Graphics g = getGraphics();
for (List<Point> points : polygons) {
Polygon polygon = new Polygon();
for (Point point : points) {
g.fillOval(point.x - 2, point.y - 2, 5, 5);
polygon.addPoint(point.x, point.y);
}
if (points.size() >= 3 && (!points.equals(polygons.get(polygons.size() - 1)) || drawLine)) {
g.drawPolygon(polygon);
}
}
g.dispose();
}

private void clear() {
Graphics g = getGraphics();
g.setColor(getBackground());
Dimension d = getSize();
g.fillRect(0, 0, d.width, d.height);
g.dispose();
}

private boolean isIntersect(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy)
{
int ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
int tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
int tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
int td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);

boolean intersect = tc * td < 0 && ta * tb < 0;
System.out.println(intersect);
return intersect;
}

@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}



}

コメント(0)

mixiユーザー
ログインしてコメントしよう!

Javaの課題丸投げ 更新情報

Javaの課題丸投げのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。

人気コミュニティランキング