Comp Form: Week 5 Assignment
Line drawing, strokes, brushes, rotation. . .
//Problem 1. Write a program that allows the user to draw a stroke of up to 1000 points. Optionally, implement a feature that prevents the addition of points that are closer than 5 pixels from the last point added.
![]()
int numPoints=0;
Vec2d pointList[1000];
void drawStroke(){
glColor3f(1,0,0);
//glLineWidth(20);
glBegin(GL_LINE_STRIP);
for ( int i=0; i<numpoints;i++){>
glVertex2f(pointList[i].x,pointList[i].y);
}
glEnd();
}</numpoints;i++){>
//Problem 2. Create a function that draws the stroke (from Problem 1) using the special wide-stroke method. The function should take as input the desired with of the stroke as a float.
![]()
void drawStrokeWeight(float strokeWeight){
glColor3f(1,0,0);
glBegin(GL_TRIANGLE_STRIP);
for ( int i=1; i<numpoints-1;i++){>
Vec2d CurrentPoint = pointList[i];
Vec2d A=pointList[i-1];
Vec2d B=pointList[i+1];
Vec2d V=B-A;
Vec2d Vp;
Vp.x=V.y*-1.0;
Vp.y=V.x;
Vp.normalize();
Vec2d C=CurrentPoint+Vp*strokeWeight/2;
Vec2d D=CurrentPoint-Vp*strokeWeight/2;
glVertex2f(C.x,C.y);
glVertex2f(D.x,D.y);
}
glEnd();
}</numpoints-1;i++){>
//Problem 3. Create a function that draws the stroke with a three-step stair-step pattern along it on just one side of the stroke.
![]()
void drawStrokeStep(float strokeWeight){
glColor3f(1,0,0);
glBegin(GL_LINE_STRIP);
for ( int i=1; i<numpoints-1;i++){>
Vec2d currentPoint = pointList[i];
Vec2d A=pointList[i-1];
Vec2d B=pointList[i+1];
Vec2d V=B-A;
Vec2d Vp;
Vp.x=V.y*-1.0;
Vp.y=V.x;
V.normalize();
Vp.normalize();
Vec2d C=currentPoint+Vp*strokeWeight;
Vec2d D=currentPoint-Vp*strokeWeight;
glVertex2f(currentPoint.x,currentPoint.y);
glVertex2f(C.x,C.y);
glVertex2f(C.x+Vp.x*strokeWeight,C.y);
glVertex2f(C.x+Vp.x*strokeWeight,C.y+Vp.y*strokeWeight);
glVertex2f(C.x+Vp.x*strokeWeight*2,C.y+Vp.y*strokeWeight);
glVertex2f(C.x+Vp.x*strokeWeight*2,C.y+Vp.y*strokeWeight*2);
}
glEnd();
}</numpoints-1;i++){>
//Problem 4. Create a function that the draws the stroke with a wave that runs perpendicular to the direction of the stroke. The wave should affect the path of the stroke, not its width. The function should take an amplitude and frequency as input
![]()
void drawStrokeWave(float amplitude, float frequency){
glColor3f(1,0,0);
glBegin(GL_LINE_STRIP);
for ( int i=1; i<numpoints-1;i++){>
Vec2d currentPoint = pointList[i];
Vec2d A=pointList[i-1];
Vec2d B=pointList[i+1];
Vec2d V=B-A;
Vec2d Vp;
Vp.x=V.y*-1.0;
Vp.y=V.x;
V.normalize();
Vp.normalize();
Vec2d C;
C.x=currentPoint.x+Vp.x*amplitude*cos(radians(i*frequency));
C.y=currentPoint.y+Vp.y*amplitude*sin(radians(i*frequency));
glVertex2f(C.x,C.y);
}
glEnd();
}</numpoints-1;i++){>
//Problem 5. Create a function that the draws the stroke with a giant arrowhead at its tip. The arrowhead should be in alignment the last stroke segment. Optionally, draw a series of similar but smaller arrows along the length of the whole stroke.
![]()
void drawStrokeArrow(float strokeWeight){
glColor3f(1,0,0);
glBegin(GL_LINES);
for ( int i=1; i<numpoints-1;i++){>
Vec2d currentPoint = pointList[i];
Vec2d A=pointList[i-1];
Vec2d B=pointList[i+1];
Vec2d V=B-A;
Vec2d Vp;
Vp.x=V.y*-1.0;
Vp.y=V.x;
V.normalize();
Vp.normalize();
Vec2d C=currentPoint+Vp*strokeWeight/2-V*strokeWeight*2;
Vec2d D=currentPoint-Vp*strokeWeight/2-V*strokeWeight*2;
glVertex2f(C.x,C.y);
glVertex2f(currentPoint.x,currentPoint.y);
glVertex2f(D.x,D.y);
glVertex2f(currentPoint.x,currentPoint.y);
}
glEnd();
}
</numpoints-1;i++){>
//Problem 7. Create a function that rotates a shape by a given angle. The function should take as input an array of points (Vec2d *), the number of points in the array (int), and an angle of rotation in degrees. You may want to calculate the center point and then rotate the points around the center, rather than about the window origin.
![]()
void rotate(Vec2d shape[], int rotation, int numPoints){
glColor3f(1,0,0);
//original shape
glBegin(GL_LINE_LOOP);
for ( int i=1; i<numpoints-1;i++){>
glVertex2f(shape[i].x,shape[i].y);
}
glEnd();
//measure center
//find average point
Vec2d center;
for (int i=0;i<numpoints;i++){>
center=center+shape[i];
}
center.x=center.x/numPoints;
center.y=center.y/numPoints;
//draw rotated
Vec2d Xp , Yp;
Xp.x=cos(radians(rotation));
Xp.y=sin(radians(rotation));
Yp.x=-sin(radians(rotation));
Yp.y=cos(radians(rotation));
glBegin(GL_LINE_LOOP);
for ( int i=1; i<numpoints-1;i++){>
Vec2d currentPoint = shape[i];
currentPoint=currentPoint-center; //translate to 0,0
Vec2d rotatedPoint=Xp*currentPoint.x+Yp*currentPoint.y;//rotate at 0,0
rotatedPoint=rotatedPoint+center;//translate to center
glVertex2f(rotatedPoint.x,rotatedPoint.y);
}
glEnd();
}</numpoints-1;i++){></numpoints;i++){></numpoints-1;i++){>
- Published:
- 10.11.07 / 11pm
- Category:
- C, Comp Form, ITP, Work in Progress
- Related: [Comp Form: Week 3][Comp Form: Week 8][Comp Form: Week 10]










No comments
Jump to comment form | comments rss [?] | trackback uri [?]