Thank you amnon.owed and tfguy44 for your helpful code examples. I have expanded upon what you taught me and came up with the following solution. What differentiates this code from the others is that it takes into account if the width is greater or less than the height, the angle of the stripes, and a special case where the width of the rectangle is smaller than the width of the stripes. Also, the given angle should be confined to be between 0 and 180 degrees.
- PGraphics pg;
-
- void setup() {
- size(600, 600);
- }
-
- void draw() {
- background(0);
- pg = createGraphics(round(200+sin(frameCount*0.02)*100), round(200+sin(frameCount*0.02*sqrt(2))*50), JAVA2D);
- drawStripes(
- color(255), // background color
- color(frameCount%255, 0, 255-frameCount%255), // stripe color
- 20+sin(frameCount*0.01)*10, // stripeWidth
- frameCount%180); // stripeAngle
- image(pg, mouseX, mouseY);
- g.removeCache(pg); //prevents out of memory error
- }
-
- void drawStripes(color bgColor, color stripeColor, float stripeWidth, float stripeAngle) {
- int numberOfStripes;
- int hyp;
- float tempAngle;
- int realLength;
- int offset;
- // Assuming a horizontal rectangle is being rotated
- if ((stripeAngle <= 45 ) || (stripeAngle > 135)) {
- if (stripeAngle <= 45) {
- hyp = round(stripeWidth/cos(radians(stripeAngle))); //How large is the stripe on the edge of the rectangle
- realLength = pg.height + 2*round(pg.width*tan(radians(stripeAngle)));
- numberOfStripes = ceil((realLength)/hyp)+1;
- }
- else {
- tempAngle = 180-stripeAngle;
- hyp = round(stripeWidth/cos(radians(tempAngle)));
- realLength = pg.height + 2*round(pg.width*tan(radians(tempAngle)));
- numberOfStripes = ceil((realLength)/hyp)+1;
- }
- offset = round((numberOfStripes-round(pg.height/hyp))/2);
- pg.beginDraw();
- pg.rectMode(CENTER);
- pg.background(bgColor);
- pg.noStroke();
- pg.fill(stripeColor);
- for (int i=0; i<=numberOfStripes; i+=2) {
- pg.pushMatrix();
- pg.translate(round(pg.width/2), (i-offset)*hyp);
- pg.rotate(radians(stripeAngle));
- pg.rect(0, 0, 2*(pg.width+stripeWidth), stripeWidth);
- pg.popMatrix();
- }
- pg.endDraw();
- }
- else {
- if (stripeAngle <= 90) {
- tempAngle = 90-stripeAngle;
- hyp = round(stripeWidth/cos(radians(tempAngle)));
- realLength = pg.width + 2*round(pg.height*tan(radians(tempAngle)));
- numberOfStripes = ceil((realLength)/hyp)+1;
- } else {
- tempAngle = stripeAngle-90;
- hyp = round(stripeWidth/cos(radians(tempAngle)));
- realLength = pg.width + 2*round(pg.height*tan(radians(tempAngle)));
- numberOfStripes = ceil((realLength)/hyp)+1;
- }
- offset = round((numberOfStripes-round(pg.width/hyp))/2);
- pg.beginDraw();
- pg.rectMode(CENTER);
- pg.background(bgColor);
- pg.noStroke();
- pg.fill(stripeColor);
- for (int i=0; i<=numberOfStripes; i+=2) {
- pg.pushMatrix();
- pg.translate((i-offset)*hyp,round(pg.height/2));
- pg.rotate(radians(stripeAngle));
- pg.rect(0, 0, 2*(pg.height+stripeWidth), stripeWidth);
- pg.popMatrix();
- }
- pg.endDraw();
- }
- }