001/* 002 003*/ 004 005package renderer.models; 006import renderer.scene.*; 007 008/** 009 Create a wireframe model of a ring (an annulus) 010 in the xy-plane centered at the origin. 011<p> 012 See <a href="https://en.wikipedia.org/wiki/Annulus_(mathematics)" target="_top"> 013 https://en.wikipedia.org/wiki/Annulus_(mathematics)</a> 014 015 @see RingSector 016*/ 017public class Ring extends Model 018{ 019 /** 020 Create a ring (annulus) in the xy-plane with outer 021 radius 1 and with inner radius 0.33, with 12 spokes 022 coming out of the center, and with 5 concentric circles. 023 */ 024 public Ring( ) 025 { 026 this(1.0, 0.33, 4, 12); 027 } 028 029 030 /** 031 Create a ring (annulus) in the xy-plane with outer 032 radius {@code r1} and with inner radius {@code r2}, 033 with 12 spokes coming out of the center, and with 034 5 concentric circles. 035 036 @param r1 outer radius of the ring 037 @param r2 inner radius of the ring 038 */ 039 public Ring(double r1, double r2) 040 { 041 this(r1, r2, 4, 12); 042 } 043 044 045 /** 046 Create a ring (annulus) in the xy-plane with outer 047 radius {@code r1} and with inner radius {@code r2}, 048 with {@code k} spokes coming out of the center, and 049 with {@code n} concentric circles (not counting the 050 inner most circle). 051 <p> 052 Notice that if there are {@code k} spokes, then each circle 053 around the center will have {@code k} line segments. If there 054 are {@code n} concentric circles around the center (not counting 055 the inner most circle), then each spoke will have {@code n} line 056 segments. 057 <p> 058 There must be at least three spokes and at least one concentric circle. 059 060 @param r1 outer radius of the ring 061 @param r2 inner radius of the ring 062 @param n number of concentric circles 063 @param k number of spokes in the ring 064 */ 065 public Ring(double r1, double r2, int n, int k) 066 { 067 super(); 068 069 if (n < 1) n = 1; 070 if (k < 3) k = 3; 071 072 // Create the rings's geometry. 073 074 double deltaR = (r1 - r2) / n; 075 double deltaTheta = (2 * Math.PI) / k; 076 077 // An array of vertices to be used to create line segments. 078 Vertex[][] v = new Vertex[n+1][k]; 079 080 // Create all the vertices. 081 for (int j = 0; j < k; j++) // choose a spoke (an angle) 082 { 083 double c = Math.cos(j * deltaTheta); 084 double s = Math.sin(j * deltaTheta); 085 for (int i = 0; i < n + 1; i++) // move along the spoke 086 { 087 double ri = r2 + i * deltaR; 088 v[i][j] = new Vertex(ri * c, 089 ri * s, 090 0); 091 } 092 } 093 094 // Create line segments around each concentric ring. 095 for (int i = 0; i < n + 1; i++) // choose a ring 096 { 097 for (int j = 0; j < k - 1; j++) 098 { 099 addLineSegment(v[i][j], v[i][j+1]); 100 } 101 // close the circle 102 addLineSegment(v[i][k-1], v[i][0]); 103 } 104 105 // Create the spokes.connecting the inner circle to the outer circle. 106 for (int j = 0; j < k; j++) // choose a spoke 107 { 108 for (int i = 0; i < n; i++) 109 { 110 addLineSegment(v[i][j], v[i+1][j]); 111 } 112 } 113 } 114}//Ring