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