drawLight method

void drawLight(
  1. Canvas canvas,
  2. Size size
)
override

Implementation

void drawLight(Canvas canvas, Size size) {
  direction.norm();
  EVector2D absolutePos = position.getAbsolutePair(size);
  double absoluteLength = length.getAbsoluteValue(size);
  double absStartPositionDist = startPositionDist.getAbsoluteValue(size);
  double absAngle = angle.getAbsoluteValue(size);
  EVector2D absPos2 = EVector2D(direction.x, direction.y);
  absPos2.rotate(90);
  absPos2.setLength(absStartPositionDist / 2);

  EVector2D curPos = absPos2.getAdd(absolutePos);
  Path p = Path();
  p.moveTo(curPos.x, curPos.y);

  //rotate direction
  //calculate the length of the rotated "sideline" so that the length of the beam will be absoluteLength
  //add this to curPos and get the pos of the new point
  double lengthOfSideLine = absoluteLength / cos(absAngle * (pi / 180));
  curPos.add((direction.getRotate(absAngle)).getMultiply(lengthOfSideLine));
  p.lineTo(curPos.x, curPos.y);
  //finished one side of the beam

  //do same steps with other side, close the path and get the beam
  absPos2.multiply(-1);
  curPos = absPos2.getAdd(absolutePos);
  curPos.add((direction.getRotate(-absAngle)).getMultiply(lengthOfSideLine));
  p.lineTo(curPos.x, curPos.y);

  curPos = absPos2.getAdd(absolutePos);
  p.lineTo(curPos.x, curPos.y);

  p.close();
  for (int i = 0; i < repainter; i++) {
    canvas.drawPath(p, _paint);
  }
}