|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +java_import 'processing.core.PConstants' |
| 4 | +# Three axes as a class |
| 5 | +class Axes |
| 6 | + SIDES = 30 |
| 7 | + ANGLE = 360 / SIDES |
| 8 | + LENGTH = 200 |
| 9 | + RADIUS = 20 |
| 10 | + |
| 11 | + attr_reader :app |
| 12 | + |
| 13 | + def initialize(app) |
| 14 | + @app = app |
| 15 | + end |
| 16 | + |
| 17 | + def draw |
| 18 | + app.fill(255, 0, 0) |
| 19 | + draw_x_axis |
| 20 | + app.fill(0, 255, 0) |
| 21 | + draw_y_axis |
| 22 | + app.fill(0, 0, 255) |
| 23 | + draw_z_axis |
| 24 | + end |
| 25 | + |
| 26 | + def draw_y_axis |
| 27 | + # draw top of the tube |
| 28 | + app.begin_shape |
| 29 | + (0..SIDES).each do |i| |
| 30 | + y = DegLut.cos(i * ANGLE) * RADIUS |
| 31 | + x = DegLut.sin(i * ANGLE) * RADIUS |
| 32 | + app.vertex(x, 0, y) |
| 33 | + end |
| 34 | + app.end_shape(PConstants::CLOSE) |
| 35 | + # draw bottom of the tube |
| 36 | + app.begin_shape |
| 37 | + (0..SIDES).each do |i| |
| 38 | + y = DegLut.cos(i * ANGLE) * RADIUS |
| 39 | + x = DegLut.sin(i * ANGLE) * RADIUS |
| 40 | + app.vertex(x, LENGTH, y) |
| 41 | + end |
| 42 | + app.end_shape(PConstants::CLOSE) |
| 43 | + # draw SIDES |
| 44 | + app.begin_shape(PConstants::TRIANGLE_STRIP) |
| 45 | + (0..SIDES).each do |i| |
| 46 | + y = DegLut.cos(i * ANGLE) * RADIUS |
| 47 | + x = DegLut.sin(i * ANGLE) * RADIUS |
| 48 | + app.vertex(x, 0, y) |
| 49 | + app.vertex(x, LENGTH, y) |
| 50 | + end |
| 51 | + app.end_shape(PConstants::CLOSE) |
| 52 | + end |
| 53 | + |
| 54 | + def draw_z_axis |
| 55 | + # draw top of the tube |
| 56 | + app.begin_shape |
| 57 | + (0..SIDES).each do |i| |
| 58 | + x = DegLut.cos(i * ANGLE) * RADIUS |
| 59 | + y = DegLut.sin(i * ANGLE) * RADIUS |
| 60 | + app.vertex(0, x, y) |
| 61 | + end |
| 62 | + app.end_shape(PConstants::CLOSE) |
| 63 | + # draw bottom of the tube |
| 64 | + app.begin_shape |
| 65 | + (0..SIDES).each do |i| |
| 66 | + x = DegLut.cos(i * ANGLE) * RADIUS |
| 67 | + y = DegLut.sin(i * ANGLE) * RADIUS |
| 68 | + app.vertex(LENGTH, x, y) |
| 69 | + end |
| 70 | + app.end_shape(PConstants::CLOSE) |
| 71 | + # draw SIDES |
| 72 | + app.begin_shape(PConstants::TRIANGLE_STRIP) |
| 73 | + (0..SIDES).each do |i| |
| 74 | + x = DegLut.cos(i * ANGLE) * RADIUS |
| 75 | + y = DegLut.sin(i * ANGLE) * RADIUS |
| 76 | + app.vertex(0, x, y) |
| 77 | + app.vertex(LENGTH, x, y) |
| 78 | + end |
| 79 | + app.end_shape(PConstants::CLOSE) |
| 80 | + end |
| 81 | + |
| 82 | + def draw_x_axis |
| 83 | + # draw top of the tube |
| 84 | + app.begin_shape |
| 85 | + (0..SIDES).each do |i| |
| 86 | + x = DegLut.cos(i * ANGLE) * RADIUS |
| 87 | + y = DegLut.sin(i * ANGLE) * RADIUS |
| 88 | + app.vertex(x, y, 0) |
| 89 | + end |
| 90 | + app.end_shape(PConstants::CLOSE) |
| 91 | + # draw bottom of the tube |
| 92 | + app.begin_shape |
| 93 | + (0..SIDES).each do |i| |
| 94 | + x = DegLut.cos(i * ANGLE) * RADIUS |
| 95 | + y = DegLut.sin(i * ANGLE) * RADIUS |
| 96 | + app.vertex(x, y, LENGTH) |
| 97 | + end |
| 98 | + app.end_shape(PConstants::CLOSE) |
| 99 | + # draw SIDES |
| 100 | + app.begin_shape(PConstants::TRIANGLE_STRIP) |
| 101 | + (0..SIDES).each do |i| |
| 102 | + x = DegLut.cos(i * ANGLE) * RADIUS |
| 103 | + y = DegLut.sin(i * ANGLE) * RADIUS |
| 104 | + app.vertex(x, y, 0) |
| 105 | + app.vertex(x, y, LENGTH) |
| 106 | + end |
| 107 | + app.end_shape(PConstants::CLOSE) |
| 108 | + end |
| 109 | +end |
0 commit comments