Statistics

Problem Statement for "Polyline"

Problem Statement

A "Polyline" is a continuous line composed of one or more line segments. In a rectangle defined by four points (0,0), (a, 0), (a, b), and (0, b), we can draw a polyline starting from the point (x0, y0) to the end point (x1, y1) with at least one intersection point with each of the four edges of the rectangle. Write a class Polyline with a method length that returns the shortest length of such a polyline.

Definition

Class:
Polyline
Method:
length
Parameters:
int, int, int, int, int, int
Returns:
double
Method signature:
double length(int a, int b, int x0, int y0, int x1, int y1)
(be sure your method is public)

Notes

  • The polyline must not exceed the boundaries of the given rectangle.
  • The starting point and ending point may overlap.
  • Your return value must have an absolute or relative error less than 1e-9.

Constraints

  • a and b will be between 2 and 200 inclusive.
  • x0 and x1 will be between 1 and (a-1) inclusive.
  • y0 and y1 will be between 1 and (b-1) inclusive.

Examples

  1. 4

    3

    1

    1

    3

    2

    Returns: 7.810249675906654

  2. 4

    3

    1

    1

    2

    2

    Returns: 8.602325267042627

  3. 4

    3

    1

    1

    1

    2

    Returns: 9.433981132056603

  4. 50

    70

    20

    40

    20

    40

    Returns: 172.04650534085255

  5. 98

    200

    78

    32

    35

    174

    Returns: 299.9549966244937

  6. 2

    2

    1

    1

    1

    1

    Returns: 5.656854249492381

  7. 2

    200

    1

    199

    1

    187

    Returns: 388.0206180088888

  8. 200

    200

    182

    28

    73

    33

    Returns: 490.6179776567508

  9. 139

    92

    83

    72

    71

    3

    Returns: 289.79475495598604

  10. 87

    33

    1

    20

    80

    27

    Returns: 111.83022847155415

  11. 100

    200

    50

    100

    49

    101

    Returns: 445.8721789930383

  12. 3

    3

    1

    1

    2

    2

    Returns: 7.0710678118654755

  13. 129

    129

    23

    24

    87

    66

    Returns: 290.33084576048753

  14. 14

    3

    13

    1

    11

    2

    Returns: 26.476404589747453

  15. 99

    9

    88

    8

    17

    2

    Returns: 127.56566936288148

  16. 200

    2

    198

    1

    12

    1

    Returns: 214.03737991294884

  17. 3

    133

    2

    20

    1

    19

    Returns: 265.04716561397146

  18. 34

    170

    21

    18

    30

    63

    Returns: 300.8421513019743

  19. 83

    171

    11

    129

    25

    38

    Returns: 293.43653487594213

  20. 10

    10

    1

    4

    9

    3

    Returns: 22.47220505424423


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: