Coord¶
Main Classes¶
Most classes in this module are collections.namedtuple objects.
| Class | Description | Attributes |
|---|---|---|
Dim |
Linear dimension | value units |
Box |
A Box | width depth |
Pad |
Padding around a tree object | prev next, parent child |
Margin |
Padding around an object | left right top bottom |
Pt |
A point in Cartesian space | x y |
Reference¶
-
exception
cpip.plot.Coord.ExceptionCoord¶ Exception class for representing Coordinates.
-
exception
cpip.plot.Coord.ExceptionCoordUnitConvert¶ Exception raised when converting units.
-
cpip.plot.Coord.BASE_UNITS= 'px'¶ Base units
-
cpip.plot.Coord.UNIT_MAP= {'pc': 12.0, None: 1.0, 'mm': 2.834645669291339, 'px': 1.0, 'in': 72.0, 'cm': 28.346456692913385, 'pt': 1.0}¶ Map of conversion factors, base unit is pixels.
-
cpip.plot.Coord.UNIT_MAP_DEFAULT_FORMAT= {'pc': '%.2f', None: '%d', 'mm': '%.1f', 'px': '%d', 'in': '%.3f', 'cm': '%.2f', 'pt': '%d'}¶ Formatting strings for writing attributes. We are trying not to write 3.999999999mm here!
-
cpip.plot.Coord.UNIT_MAP_DEFAULT_FORMAT_WITH_UNITS= {'pc': '%.2f%s', None: '%d%s', 'mm': '%.1f%s', 'px': '%d%s', 'in': '%.3f%s', 'cm': '%.2f%s', 'pt': '%d%s'}¶ Map of formatting strings for value and units e.g. to create ‘0.667in’ from (2.0 / 3.0, ‘in’)
-
cpip.plot.Coord.units()¶ Returns the unsorted list of acceptable units.
-
cpip.plot.Coord.convert(val, unitFrom, unitTo)¶ Convert a value from one set of units to another.
Parameters: - val (
float, int) – The value - unitFrom (
NoneType, str) – The initial units. - unitTo (
str) – The new units.
Returns: float,int– The value in the new units.- val (
-
class
cpip.plot.Coord.Dim¶ Represents a dimension as an engineering value i.e. a number and units.
-
scale(factor)¶ Returns a new Dim() scaled by a factor, units are unchanged.
-
convert(u)¶ Returns a new Dim() with units changed and value converted.
-
__add__(other)¶ Overload self+other, returned result has the sum of self and other. The units chosen are self’s unless self’s units are None in which case other’s units are used (if not None).
-
__sub__(other)¶ Overload self-other, returned result has the difference of self and other. The units chosen are self’s unless self’s units are None in which case other’s units are used (if not None).
-
__iadd__(other)¶ Addition in place, value of other is converted to my units and added.
-
__isub__(other)¶ Subtraction in place, value of other is subtracted.
-
__lt__(other)¶ Returns true if self value < other value after unit conversion.
-
__le__(other)¶ Returns true if self value <= other value after unit conversion.
-
__eq__(other)¶ Returns true if self value == other value after unit conversion.
-
__ne__(other)¶ Returns true if self value != other value after unit conversion.
-
__gt__(other)¶ Returns true if self value > other value after unit conversion.
-
__ge__(other)¶ Returns true if self value >= other value after unit conversion.
-
-
class
cpip.plot.Coord.Pad¶ Padding around another object that forms the Bounding Box. All 4 attributes are Dim() objects
-
__str__()¶ Stringifying.
-
-
class
cpip.plot.Coord.Pt¶ A point, an absolute x/y position on the plot area. Members are Coord.Dim().
-
__eq__(other)¶ Comparison.
-
__str__()¶ Stringifying.
-
convert(u)¶ Returns a new Pt() with units changed and value converted.
-
scale(factor)¶ Returns a new Pt() scaled by a factor, units are unchanged.
-
-
cpip.plot.Coord.baseUnitsDim(theLen)¶ Returns a Coord.Dim() of length and units BASE_UNITS.
Parameters: theLen ( float, int) – Length.Returns: cpip.plot.Coord.Dim([float, str])– A new dimension of theLen in base units.
-
cpip.plot.Coord.zeroBaseUnitsDim()¶ Returns a Coord.Dim() of zero length and units BASE_UNITS.
Returns: cpip.plot.Coord.Dim([float, str])– A new dimension of zero.
-
cpip.plot.Coord.zeroBaseUnitsBox()¶ Returns a Coord.Box() of zero dimensions and units BASE_UNITS.
-
cpip.plot.Coord.zeroBaseUnitsPad()¶ Returns a Coord.Pad() of zero dimensions and units BASE_UNITS.
-
cpip.plot.Coord.zeroBaseUnitsPt()¶ Returns a Coord.Dim() of zero length and units BASE_UNITS.
Returns: cpip.plot.Coord.Pt([cpip.plot.Coord.Dim([float, str]), cpip.plot.Coord.Dim([float, str])])– A new point with the values [0, 0].
-
cpip.plot.Coord.newPt(theP, incX=None, incY=None)¶ Returns a new Pt object by incrementing existing point incX, incY that are both Dim() objects or
None.Parameters: - theP (
cpip.plot.Coord.Pt([cpip.plot.Coord.Dim([float, str]), cpip.plot.Coord.Pt([cpip.plot.Coord.Dim([float, str])])) – The initial point. - incX (
NoneType, cpip.plot.Coord.Dim([float, str]), cpip.plot.Coord.Dim([int, str])) – Distance to move in the x axis. - incY (
NoneType, cpip.plot.Coord.Dim([float, str]), cpip.plot.Coord.Dim([int, str])) – Distance to move in the y axis.
Returns: cpip.plot.Coord.Pt([cpip.plot.Coord.Dim([float, str]), cpip.plot.Coord.Dim([float, str])])– The new point.- theP (
-
cpip.plot.Coord.convertPt(theP, theUnits)¶ Returns a new point with the dimensions of theP converted to theUnits.
TODO: Deprecate this.
Examples¶
Coord.Dim()¶
Creation, addition and subtraction:
d = Coord.Dim(1, 'in') + Coord.Dim(18, 'px')
# d is 1.25 inches
d = Coord.Dim(1, 'in') - Coord.Dim(18, 'px')
# d is 0.75 inches
d += Coord.Dim(25.4, 'mm')
# d is 1.75 inches
Scaling and unit conversion returns a new object:
a = Coord.Dim(12, 'px')
b = myObj.scale(6.0)
# b is 72 pixels
c = b.convert('in')
# 1 is 1 inch
Comparison:
assert(Coord.Dim(1, 'in') == Coord.Dim(72, 'px'))
assert(Coord.Dim(1, 'in') >= Coord.Dim(72, 'px'))
assert(Coord.Dim(1, 'in') <= Coord.Dim(72, 'px'))
assert(Coord.Dim(1, 'in') > Coord.Dim(71, 'px'))
assert(Coord.Dim(1, 'in') < Coord.Dim(73, 'px'))
Coord.Pt()¶
Creation:
p = Coord.Pt(
Coord.Dim(12, 'px'),
Coord.Dim(24, 'px'),
)
print(p)
# Prints: 'Pt(x=Dim(12px), y=Dim(24px))'
p.x # Coord.Dim(12, 'px'))
p.y # Coord.Dim(24, 'px'))
# Scale up by 6 and convert units
pIn = p.scale(6).convert('in')
# pIn now 'Pt(x=Dim(1in), y=Dim(2in))'
Testing¶
The unit tests are in test/TestCoord.py.