/
PlotUtil.py
54 lines (44 loc) · 1.71 KB
/
PlotUtil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
# See AUTHORS.txt
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
# you can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
import numpy as np
class PlotUtil:
@staticmethod
def format_value_unit(value, unit):
if isinstance(value, float):
return "{:.2f} {}".format(value, unit)
elif isinstance(value, int):
return "{:d} {}".format(value, unit)
else:
return "{} {}".format(value, unit)
@staticmethod
def middle_from_points(x1, y1, x2, y2):
return (x1 + x2) * 0.5, (y1 + y2) * 0.5
@staticmethod
def vec_from_points(x1, y1, x2, y2):
return x2 - x1, y2 - y1
@staticmethod
def mag_from_points(x1, y1, x2, y2):
x, y = PlotUtil.vec_from_points(x1, y1, x2, y2)
return np.linalg.norm([x, y])
@staticmethod
def norm_from_points(x1, y1, x2, y2):
x, y, = PlotUtil.vec_from_points(x1, y1, x2, y2)
return PlotUtil.norm_from_vec(x, y)
@staticmethod
def norm_from_vec(x, y):
n = np.linalg.norm([x, y])
return x / n, y / n
@staticmethod
def orth_from_points(x1, y1, x2, y2):
x, y = PlotUtil.vec_from_points(x1, y1, x2, y2)
return -y, x
@staticmethod
def orth_norm_from_points(x1, y1, x2, y2):
x, y = PlotUtil.vec_from_points(x1, y1, x2, y2)
xn, yn = PlotUtil.norm_from_vec(x, y)
return -yn, xn