Skip to content

Commit c14e08d

Browse files
committed
Refactor draw_polar_axes method to support multiple passes and improve axis drawing logic
1 parent edcdc3a commit c14e08d

File tree

1 file changed

+58
-18
lines changed

1 file changed

+58
-18
lines changed

lib/gr/plot.rb

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,9 @@ def draw_axes(kind, pass = 1)
502502
end
503503
end
504504

505-
def draw_polar_axes
505+
def draw_polar_axes(pass = 1)
506506
viewport = kvs[:viewport]
507+
vp = kvs[:vp]
507508
diag = Math.sqrt((viewport[1] - viewport[0])**2 + (viewport[3] - viewport[2])**2)
508509
charheight = [0.018 * diag, 0.012].max
509510

@@ -516,28 +517,67 @@ def draw_polar_axes
516517
GR.setlinetype(GR::LINETYPE_SOLID)
517518

518519
tick = auto_tick(rmin, rmax)
519-
n = ((rmax - rmin) / tick + 0.5).round
520-
(n + 1).times do |i|
521-
r = i.to_f / n
522-
if i.even?
520+
n = ((rmax - rmin) / tick).truncate
521+
if n <= 4
522+
tick /= 2.0
523+
n *= 2
524+
end
525+
526+
if pass == 1
527+
GR.selntran(1)
528+
(n + 1).times do |i|
529+
r = i.to_f * tick / (rmax - rmin)
530+
if r > 0 && r < 1
531+
if i.even?
532+
GR.setlinecolorind(88)
533+
GR.drawarc(-r, r, -r, r, 0, 360)
534+
else
535+
GR.setlinecolorind(90)
536+
GR.drawarc(-r, r, -r, r, 0, 360)
537+
end
538+
end
539+
end
540+
GR.setclip(0)
541+
GR.setlinecolorind(88)
542+
GR.drawarc(-1, 1, -1, 1, 0, 360)
543+
544+
GR.setclip(1)
545+
sign = (kvs[:theta_direction] || 1) > 0 ? 1 : -1
546+
offs = THETA_ZERO_LOCATION[kvs[:theta_zero_location] || 'E']
547+
0.step(by: 45, to: 315) do |alpha|
548+
sinf = Math.sin((alpha * sign) * Math::PI / 180 + offs)
549+
cosf = Math.cos((alpha * sign) * Math::PI / 180 + offs)
523550
GR.setlinecolorind(88)
524-
GR.drawarc(-r, r, -r, r, 0, 359) if i > 0
551+
GR.polyline([cosf, 0], [sinf, 0])
552+
GR.settextalign(GR::TEXT_HALIGN_CENTER, GR::TEXT_VALIGN_HALF)
553+
x, y = GR.wctondc(1.1 * cosf, 1.1 * sinf)
554+
GR.textext(x, y, "#{alpha}^o")
555+
end
556+
557+
if kvs.has_key?(:title)
558+
GR.settextalign(GR::TEXT_HALIGN_CENTER, GR::TEXT_VALIGN_TOP)
559+
text(0.5 * (viewport[0] + viewport[1]), vp[3] - 0.02, kvs[:title].to_s)
560+
end
561+
end
562+
563+
if pass == 2
564+
start = (rmin / tick).floor.truncate
565+
(n + 1).times do |i|
566+
j = start + i
567+
next unless j * tick >= rmin
568+
569+
r = i.to_f * tick / (rmax - rmin)
570+
next unless i.even?
571+
525572
GR.settextalign(GR::TEXT_HALIGN_LEFT, GR::TEXT_VALIGN_HALF)
526573
x, y = GR.wctondc(0.05, r)
527-
GR.text(x, y, (rmin + i * tick).to_s) # FIXME: round. significant digits.
528-
else
529-
GR.setlinecolorind(90)
530-
GR.drawarc(-r, r, -r, r, 0, 359)
574+
# fmt = GR.getformat(start, rmin, rmax, tick, 2)
575+
# s = GR.ftoa(j * tick, fmt)
576+
s = (j * tick).to_s # Fallback
577+
GR.text(x, y, s)
531578
end
532579
end
533-
0.step(by: 45, to: 315) do |alpha|
534-
sinf = Math.sin(alpha * Math::PI / 180)
535-
cosf = Math.cos(alpha * Math::PI / 180)
536-
GR.polyline([cosf, 0], [sinf, 0])
537-
GR.settextalign(GR::TEXT_HALIGN_CENTER, GR::TEXT_VALIGN_HALF)
538-
x, y = GR.wctondc(1.1 * cosf, 1.1 * sinf)
539-
GR.textext(x, y, "#{alpha}^o")
540-
end
580+
541581
GR.restorestate
542582
end
543583

0 commit comments

Comments
 (0)