Monday, January 26, 2009

Finished Bezier Curve Algorithms

Alright, I finally debugged and finished my algorithm for cubic and quadratic Bezier curves. Below is a pic of the applet running the program.
Here is the code used:

public void setup(){
size(250,300);
background(0);
}

public void draw(){
stroke(0,255,0);
myCurve(25, 100, 50, 210, 200, 100, 200, 30, 0.01);
stroke(255,0,0);
myCurve(50, 210, 200, 100, 200, 30, 0.01);
stroke(0,0,255);
myCurve(200, 100, 200, 30, 0.01);
}

public void myCurve(int x0, int y0, int x1, int y1, int x2, int y2, double t){
//Quadratic
double x = x0;
double y = y0;
point(x0, y0);
for(double i = t; i <= 1; i+=t){
//System.out.println(i);
x = Math.pow((1.0-i),2)*x0 + 2.0*(1.0-i)*i*x1 + i*i*x2;
y = Math.pow((1.0-i),2)*y0 + 2.0*(1.0-i)*i*y1 + i*i*y2;
point(Math.round(x),Math.round(y));
}
point(x2,y2);
}

public void myCurve(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3, double t){
//cubic
double x = x0;
double y = y0;
point(x0, y0);
for(double i = t; i <= 1; i+=t){
x = Math.pow((1.0-i),3)*x0 + Math.pow((1-i),2)*3*i*x1 + Math.pow((1-i),2)*3*i*i*x2 + i*i*i*x3;
y = Math.pow((1.0-i),3)*y0 + Math.pow((1-i),2)*3*i*y1 + Math.pow((1-i),2)*3*i*i*y2 + i*i*i*y3;
point(Math.round(x),Math.round(y));
}
point(x3, y3);
}

public void myCurve(int x0, int y0, int x1, int y1, double t){
//linear
double x = x0;
double y = y0;
point(x0, y0);
for(double i = 0; i <= 1; i+=t){
x += (x1-x0)*t;
y += (y1-y0)*t;
point(Math.round(x),Math.round(y));
}
point(x1, y1);
}

Thursday, January 22, 2009

For the third assignment, I was able to successfully create the parametric lines. I am still working on the quadratic and cubic curves. Here is the code for the new myLine method.

public void myLine(int x0, int y0, int x1, int y1, double t){

double x = x0;
double y = y0;
for(double i = 0; i <= 1; i+=t){
if(i == 0){
point(x0, y0);
}else if (i == 1){
point(x1, y1);
}else{
x+= (x1-x0)*t;
y+= (y1-y0)*t;
point(Math.round(x),Math.round(y));
}
}
}

Tuesday, January 13, 2009

Second Homework Assignment

I have finished the second homework assignment due January 16th. I was able to create my own implementations of the line function, the circle function and created a web-like design on the corner of the applet box. The following screen capture shows all three.

The Wikipedia articles were extremely helpful for writing out these programs.

I may try out the anti-aliasing bonus later, but I figured I might as well post this finished work now.

Here is the code.

//Programmed by Brett J. Young
//Creates a house

import processing.core.*;
public class Driver extends PApplet{

public void setup(){
size(250,300);
background(0,100,210);
}

public void draw(){

stroke(255);
myLine(20,230,100,120);
stroke(100);
myWeb(135,7);
stroke(0);
myCircle(150,150,20);
}

public void myCircle(int x0, int y0, int rad){
int f = 1 - rad;
int dx = 1;
int dy = -2 * rad;
int x = 0;
int y = rad;

point(x0, y0 + rad);
point(x0, y0 - rad);
point(x0 + rad, y0);
point(x0 - rad, y0);

while(x if(f >= 0){
y--;
dy += 2;
f += dy;
}
x++;
dx += 2;
f += dx;

point(x0 + x, y0 + y);
point(x0 - x, y0 + y);
point(x0 + x, y0 - y);
point(x0 - x, y0 - y);
point(x0 + y, y0 + x);
point(x0 - y, y0 + x);
point(x0 + y, y0 - x);
point(x0 - y, y0 - x);

}
}

public void myWeb(int size, int step){
for(int i = 0; i < size; i+=step){
int q = size - i;
myLine(0,q,i,0);
}
}

public void myLine(int xa, int ya, int xb, int yb){
int x0 = xa;
int x1 = xb;
int y0 = ya;
int y1 = yb;

boolean steep = (Math.abs(y1 - y0) > Math.abs(x1 - x0));
if(steep){
//swap(x0, y0)
x0 = ya;
y0 = xa;
//swap (x1,y1)
x1 = yb;
y1 = xb;
if(x0>x1){
//swap(x0,x1)
x0 = yb;
x1 = ya;
//swap (y0,y1)
y0 = xb;
y1 = xa;
}
}
else if(x0>x1){
//swap(x0,x1)
x0 = xb;
x1 = xa;
//swap (y0,y1)
y0 = yb;
y1 = ya;
}

int deltax = x1 - x0;
int deltay = Math.abs(y0 - y1);

double error = 0;
double deltaerror = (Double)(1.0 * deltay) / deltax;

int ystep;

int y = y0;

if(y0 < y1){
ystep = 1;
}else{
ystep = -1;
}

for(int x = x0; x < x1; x++){
if(steep){
point(y,x);
}else{
point(x,y);
}

error += deltaerror;

if(error >= 0.5){
y += ystep;
error -= 1.0;
}
}


}

}

Wednesday, January 7, 2009

First assignment is complete.

For the introductory assignment, I downloaded the Processing library and followed the tutorial to use it through Eclipse.












The script builds a small, flat-roofed house on a light blue background. It kind of looks like this.


The source for the file can be found on the UNCW TIMMY FTP at the following location:

ftp://bjy3120@studentfiles.uncw.edu/CSC370/MyProcessingSketch.java

First Post

This is the official first post of my CSC 370 CourseBlog! Please note that this is not your standard blog - it's a CourseBlog.

CourseBlog ('kôrs bläg) n. An electronic log of all work performed in a specified class.

This first post shall act primarily as an introduction, but it will also serve the following very important tasks:

1: Allow me to play with my font settings and overall look and feel for this CourseBlog

2: Allow me to waste a little time in the hour I have between CSC 370 and my next class (CSC 342).

3: Get that obnoxious "F1R57 P05T!!!!!1!!11!!!!1!!" mess out of the way without interfering with any actual work.

4. Establish the possibility of supplementing this CourseBlog with some vlogging from my YouTube account.

(Oh yeah, I may supplement my CourseBlog with a vlog on my YouTube account. I'll post the videos here)

5. Try to give the reader a sort of first glimpse* of my writing style and personality.

Now that the first post is out of the way, I can go ahead and start treating all following posts with the same rigor and protocol of just any old posts.

Man, I can't wait to call second postz!

*more accurately translates to "warning"