找出线路径中的匝数

时间:2019-05-31 08:54:02

标签: java

如何找出随机线路径中的匝数?由随机位置的4条线形成的线路径。每条线的“终点”是下一条线的“起点”。

我已经尝试了一些基于坐标的计算,还尝试了使用基于角度和角度的计算。

public void drawRect(Graphics g){         g2d =(Graphics2D)g;         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);         g2d.setColor(Color.blue);         g2d.setStroke(new BasicStroke(1));         rect =新的Rectangle2D.Float(200,75,400,250);         g2d.draw(rect);     }

////////////////////////////////////////////////////////////////////
//GET RECTANGLE BOX X1,Y1,X2,Y2 POSITION AND SEND TO RANDOM METHOD
//FOR GET THE NEW POINT VALUE (Randomized) for creation of new line 
////////////////////////////////////////////////////////////////////

public int MinX(){
    return (int) rect.getMinX()+10;
}
public int MaxX(){
    return (int) rect.getMaxX()-50;
}
public int MinY(){
    return (int) rect.getMinY()+10;
}
public int MaxY(){
    return (int) rect.getMaxY()-30;
}
public int RandX(){
    return MinX() + (int) ( Math.random() * ((MaxX() - MinX()) + 1) );
}
public int RandY(){
    return MinY() + (int) ( Math.random() * ((MaxY() - MinY()) + 1) );
}
//get the type of turn 
public String RandTurn(){
    int r = new Random().nextInt(turn.length);  //get random value from string array
    return turn[r];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////END OF FINDING RANDX, RANDY POSITION
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void drawLines(Graphics g, int counter, String turnV){
    g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);    
    Font font = g.getFont().deriveFont(Font.BOLD, 20.0f);
    g2d.setFont(font);
    g2d.drawString("Qno. "+String.valueOf(counter)+". ", 100, 50);
    g2d.drawString(""+turnV , 400, 50);
    g2d.setStroke(new BasicStroke(3));


    //1st line

    originPT = new Point(500, 200); 

    //FIND OUT THE RANDOM POSITION FOR LINE1 X2,Y2
    randX = RandX();
    randY = RandY();
    endPT = new Point(randX,randY);//POSITION: X2,Y2
    g2d.setColor ( Color.BLUE);
    g2d.draw(new Line2D.Double(originPT, endPT));

    //2nd line
    randX = RandX();
    randY = RandY();
    originPT = new Point(randX,randY);    //POSITION: X2,Y2
    g2d.setColor ( Color.red);
    g2d.draw(new Line2D.Double(endPT, originPT));    //GET X2,Y2 VALUES FROM LINE1 FOR X1,Y1

    //3rd line - Destination
    randX = RandX();
    randY = RandY();
    endPT = new Point(randX, randY); //POSITION X2, Y2
    g2d.setColor ( Color.green);
    g2d.draw(new Line2D.Double(originPT, endPT)); //GET X2, Y2 POSITION OF LINE 2 FOR X1, Y1

    //4th line
    randX = RandX();
    randY = RandY();
    originPT = new Point(randX, randY); //POSITION X2, Y2
    g2d.setColor ( Color.black);
    g2d.draw(new Line2D.Double(endPT, originPT)); //GET X2, Y2 POSITION OF LINE 3 
    //arrow head
    drawArrowHead(g2d, originPT, endPT, Color.black);    

}

我的代码成功运行,但是我的问题是,如何找出并计算总的右转和左转?请帮助我。

1 个答案:

答案 0 :(得分:0)

这些布尔方法可以帮助您解决问题。 他们假定您从点a开始,转到点b,然后再转到点c,并且y轴是反向的(指向下方)。

boolean isRightTurn(Point a, Point b, Point c) {
  return a.x * (b.y - c.y) + b.x * (c.y - a.y) +  c.x * (a.y - b.y) > 0;
}

boolean isLeftTurn(Point a, Point b, Point c) {
  return a.x * (b.y - c.y) + b.x * (c.y - a.y) +  c.x * (a.y - b.y) < 0;
}

要获得向右转或向左转的次数,您必须在每一转弯处检查是否为左转或向右转,然后对它们进行计数。