void setup() {
size(800, 550);
background(0);
draw_text(20, 40);
draw_line(10, 5);
calculat(150,80);
}
void calculat(int X,int Y){
//set base value
float beginBalance = 6000;
float interestRPM =0.12/12; // IRPM is interestRatePerMonth because rate is 12 per year so per month is 12/12
float interest =0;
float payment =beginBalance*(interestRPM/(1-pow(1+interestRPM,-12)));
float prinCipal = 0;
float endingBalance =0;
float sumInterest =0;
int count = 1;
while(count<=12){
interest = interestRPM*beginBalance;
prinCipal = payment - interest;
endingBalance = beginBalance - prinCipal;
sumInterest = sumInterest + interest;
text(nf(+beginBalance,3,1),X,Y);
text(nf(+interest,2,1),X+130,Y);
text(nf(+prinCipal,3,1),X+220,Y);
text(nf(+endingBalance,3,1),X+350,Y);
text(nf(+sumInterest,3,1),X+530,Y);
beginBalance = endingBalance;
Y += 40;
count ++ ;
}
}
// function draw every text
void draw_text(int X, int Y) {
int month=1;// set base value month
textSize(16);
fill(255, 0, 0);
text("Month BeginningBalance Interest Principal EndingBalance InterestSum", X, Y);
text_month(X+20,Y+40,month);
}
// function draw line
void draw_line(int X, int Y) {
line_holizon(X, Y);
line_vertical(X, Y);
}
// draw line vertical
void line_vertical(int X, int Y) {
stroke(0, 0, 200);
line(X+70, 0, X+70, 550);
line(X+250, 0, X+250, 550);
line(X+340, 0, X+340, 550);
line(X+440, 0, X+440, 550);
line(X+640, 0, X+640, 550);
}
// draw line holizon
void line_holizon(int X, int Y) {
while (Y<550) {
stroke(255);
line(0, Y, 800, Y);
Y+=40;
}
}
// function draw number month
void text_month(int X,int Y,int month){
fill(255);
while(month<=12){
text(month,X,Y);
Y+=40;
month++;
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น