Statistics

Problem Statement for "HexagonIntersections"

Problem Statement

A hexagonal grid is a tiling of the plane consisting solely of regular hexagons. You will be given two pairs of coordinates, identifying two hexagons in the grid. Consider a line segment connecting the centers of these two hexagons, and return the number of hexagons that this line segment intersects.

Only count hexagons if the line passes through their interior. Do not count a hexagon if the line only intersects one of its corners or is coincident with one of its edges.

Hexagons will be identified by a pair of coordinates, as shown in the following diagram. The first coordinate is proportional to the horizontal distance from the vertical axis. The second coordinate is proportional to the vertical distance from the diagonal axis.

Definition

Class:
HexagonIntersections
Method:
count
Parameters:
int, int, int, int
Returns:
int
Method signature:
int count(int x0, int y0, int x1, int y1)
(be sure your method is public)

Constraints

  • x0, y0, x1, and y1 will be between -10000 and 10000, inclusive.
  • (x0,y0) and (x1,y1) will not be the same point.

Examples

  1. 1

    -2

    3

    1

    Returns: 4

    A line between the centers of hexagons (1,-2) and (3,1) intersects 4 hexagons:

  2. 0

    0

    1

    0

    Returns: 2

  3. 0

    0

    1

    1

    Returns: 2

  4. 0

    0

    0

    1

    Returns: 2

  5. 0

    0

    -1

    0

    Returns: 2

  6. 0

    0

    -1

    -1

    Returns: 2

  7. 0

    0

    0

    -1

    Returns: 2

  8. 11

    10

    10

    10

    Returns: 2

  9. 11

    11

    10

    10

    Returns: 2

  10. 10

    11

    10

    10

    Returns: 2

  11. 9

    10

    10

    10

    Returns: 2

  12. 9

    9

    10

    10

    Returns: 2

  13. 10

    9

    10

    10

    Returns: 2

  14. 4

    2

    0

    0

    Returns: 3

  15. 2

    4

    0

    0

    Returns: 3

  16. -2

    2

    0

    0

    Returns: 3

  17. -4

    -2

    0

    0

    Returns: 3

  18. -2

    -4

    0

    0

    Returns: 3

  19. 2

    -2

    0

    0

    Returns: 3

  20. 0

    0

    4

    -1

    Returns: 6

  21. 0

    0

    10

    2

    Returns: 11

  22. 500

    500

    505

    504

    Returns: 6

  23. 3000

    -2000

    3012

    -1985

    Returns: 16

  24. 0

    0

    1

    5

    Returns: 6

  25. 0

    0

    -4

    16

    Returns: 21

  26. 4

    5

    -1

    1

    Returns: 6

  27. 20

    0

    0

    -25

    Returns: 26

  28. -10000

    -10000

    10000

    10000

    Returns: 20001

  29. -10000

    -10000

    10000

    9999

    Returns: 26666

  30. -10000

    -10000

    9999

    9998

    Returns: 26666

  31. -10000

    10000

    10000

    -10000

    Returns: 20001

  32. -9999

    10000

    10000

    -10000

    Returns: 40000

  33. -9999

    9998

    10000

    -10000

    Returns: 39998

  34. 9284

    3473

    2374

    8372

    Returns: 12480

  35. -2375

    9374

    283

    5478

    Returns: 6967

  36. -10000

    51

    10000

    51

    Returns: 20001

  37. -10000

    -51

    10000

    -52

    Returns: 26668

  38. -2004

    -1002

    -2000

    -1000

    Returns: 3

    This line is coincident with the edges of several hexagons, but it only intersects the interior of three:

  39. 54

    93

    64

    95

    Returns: 11

  40. 0

    0

    19

    20

    Returns: 26

  41. 0

    0

    19

    -20

    Returns: 40

  42. 9999

    3456

    -9999

    -9999

    Returns: 22294

  43. 0

    0

    1

    10000

    Returns: 13334


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: