Skip to content

Commit

Permalink
fixes for Plot[]
Browse files Browse the repository at this point in the history
  • Loading branch information
poke1024 committed Aug 22, 2016
1 parent 07b2197 commit d2d478d
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions mathics/builtin/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ def coords(value):


class Coords(object):
def __init__(self, graphics, expr=None, pos=None, d=None):
def __init__(self, graphics, expr=None, pos=None):
self.graphics = graphics
self.p = pos
self.d = d
if expr is not None:
if expr.has_form('Offset', 1, 2):
self.d = coords(expr.leaves[0])
Expand All @@ -72,17 +71,29 @@ def __init__(self, graphics, expr=None, pos=None, d=None):
def pos(self):
p = self.p
p = (cut(p[0]), cut(p[1]))
if self.d is not None:
d = self.graphics.translate_absolute(self.d)
return p[0] + d[0], p[1] + d[1]
else:
return p
return p

def add(self, x, y):
p = (self.p[0] + x, self.p[1] + y)
return Coords(self.graphics, pos=p, d=self.d)


class AxisCoords(Coords):
def __init__(self, graphics, expr=None, pos=None, d=None):
super(AxisCoords, self).__init__(graphics, expr=expr, pos=pos)
self.d = d

def pos(self):
p = self.p
p = self.graphics.translate(p)
p = (cut(p[0]), cut(p[1]))
if self.d is not None:
d = self.graphics.translate_absolute_in_pixels(self.d)
return p[0] + d[0], p[1] + d[1]
else:
return p


def cut(value):
"Cut values in graphics primitives (not displayed otherwise in SVG)"
border = 10 ** 8
Expand Down Expand Up @@ -1147,7 +1158,6 @@ def patch_transforms(self, transforms):

def extent(self):
def points():
#fixed_transforms = [self.graphics.fix_transform(transform) for transform in self.transforms]
for content in self.contents:
for transform in self.transforms:
p = content.extent()
Expand All @@ -1157,7 +1167,6 @@ def points():

def to_svg(self):
def instances():
# fixed_transforms = [self.graphics.fix_transform(transform) for transform in self.transforms]
for content in self.contents:
content_svg = content.to_svg()
for transform in self.transforms:
Expand Down Expand Up @@ -1430,18 +1439,27 @@ def set_size(self, xmin, ymin, extent_width, extent_height, pixel_width, pixel_h
self.elements[0].patch_transforms([transform])
self.local_to_world = transform

def add_axis_element(self, e):
# axis elements are added after the GeometricTransformationBox and are thus not
# subject to the transformation from local to pixel space.
self.elements.append(e)

def translate(self, coords):
if self.local_to_world:
return list(self.local_to_world.transform([coords]))[0]
else:
return coords[0], coords[1]

def translate_absolute(self, d):
s = self.extent_width / self.pixel_width
x, y = self.translate_absolute_in_pixels(d)
return x * s, y * s

def translate_absolute_in_pixels(self, d):
if self.local_to_world is None:
return 0, 0
else:
s = self.extent_width / self.pixel_width # d is a pixel size
l = s * 96.0 / 72
l = 96.0 / 72 # d is measured in printer's points
return d[0] * l, (-1 if self.neg_y else 1) * d[1] * l

def translate_relative(self, x):
Expand Down Expand Up @@ -1800,7 +1818,7 @@ def create_axes(self, elements, graphics_options, xmin, xmax, ymin, ymax):

def add_element(element):
element.is_completely_visible = True
elements.elements.append(element)
elements.add_axis_element(element)

ticks_x, ticks_x_small, origin_x = self.axis_ticks(xmin, xmax)
ticks_y, ticks_y_small, origin_y = self.axis_ticks(ymin, ymax)
Expand All @@ -1820,26 +1838,24 @@ def add_element(element):
if axes[index]:
add_element(LineBox(
elements, axes_style[index],
lines=[[Coords(elements, pos=p_origin(min),
d=p_other0(-axes_extra)),
Coords(elements, pos=p_origin(max),
d=p_other0(axes_extra))]]))
lines=[[AxisCoords(elements, pos=p_origin(min), d=p_other0(-axes_extra)),
AxisCoords(elements, pos=p_origin(max), d=p_other0(axes_extra))]]))
ticks_lines = []
tick_label_style = ticks_style[index].clone()
tick_label_style.extend(label_style)
for x in ticks:
ticks_lines.append([Coords(elements, pos=p_origin(x)),
Coords(elements, pos=p_origin(x),
ticks_lines.append([AxisCoords(elements, pos=p_origin(x)),
AxisCoords(elements, pos=p_origin(x),
d=p_self0(tick_large_size))])
add_element(InsetBox(
elements, tick_label_style,
content=Real('%g' % x), # fix e.g. 0.6000000000000001
pos=Coords(elements, pos=p_origin(x),
pos=AxisCoords(elements, pos=p_origin(x),
d=p_self0(-tick_label_d)), opos=p_self0(1)))
for x in ticks_small:
pos = p_origin(x)
ticks_lines.append([Coords(elements, pos=pos),
Coords(elements, pos=pos,
ticks_lines.append([AxisCoords(elements, pos=pos),
AxisCoords(elements, pos=pos,
d=p_self0(tick_small_size))])
add_element(LineBox(elements, axes_style[0],
lines=ticks_lines))
Expand Down

0 comments on commit d2d478d

Please sign in to comment.