Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function computeCode(x, y){
- x = parseInt(x);
- y = parseInt(y);
- var code = INSIDE;
- if(x < clip[0]){
- code |= LEFT;
- }else if(x > clip[2]){
- code |= RIGHT;
- }
- if(y < clip[1]){
- code |= BOTTOM;
- }else if(y > clip[3]){
- code |= TOP;
- }
- return code;
- }
- function clipping(){
- for(var k = 0;k < loop;k++){
- var code1 = computeCode(xAwal[k], yAwal[k]);
- var code2 = computeCode(xAkhir[k], yAkhir[k]);
- console.log(code1);
- console.log(code2);
- var x1 = parseInt(xAwal[k]), x2 = parseInt(xAkhir[k]), y1 = parseInt(yAwal[k]), y2 = parseInt(yAkhir[k]);
- var accept = false
- while(true){
- temp = code1 & code2;
- if((code1 == 0) && (code2 == 0)){
- accept = true;
- break;
- }else if(temp){
- break;
- }else{
- var code_out;
- var x, y;
- if(code1 != 0){
- code_out = code1;
- }else{
- code_out = code2;
- }
- if(code_out & TOP){
- x = x1 + (x2 - x1) * (clip[3] - y1) / (y2 - y1);
- y = clip[3];
- }else if(code_out & BOTTOM){
- x = x1 + (x2 - x1) * (clip[1] - y1) / (y2 - y1);
- y = clip[1];
- }else if(code_out & RIGHT){
- y = y1 + (y2 - y1) * (clip[2] - x1) / (x2 - x1);
- x = clip[2];
- }else if(code_out & LEFT){
- y = y1 + (y2 - y1) * (clip[0] - x1) / (x2 - x1);
- x = clip[0];
- }
- if(code_out == code1){
- xAwal[k] = x;
- yAwal[k] = y;
- code1 = computeCode(x, y);
- } else {
- xAkhir[k] = x;
- yAkhir[k] = y;
- code2 = computeCode(x, y);
- }
- console.log(x);
- console.log(y);
- }
- }
- console.log(code1);
- console.log(code2);
- if(!accept){
- xAwal[k] = 0;
- xAkhir[k] = 0;
- yAwal[k] = 0;
- yAkhir[k]= 0;
- }
- }
- document.getElementById('input-data').innerHTML = "";
- drawAfterClipping();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement