Hello! I recently started learning java. Help please with the translation of this code from c ++ to java. It is necessary to write a program that performs an external window clipping. The program should be based on the algorithm of clipping the rectangular Sutherland-Cowan window. I could write it on c ++, but on java I have no idea how.
What I have tried:
<pre>#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
#define LEFT 1
#define RIGHT 2
#define BOT 4
#define TOP 8
#define vcode(r, p)((((p)->x < (r)->x_min) ? LEFT : 0)+ (((p)->x > (r)->x_max) ? RIGHT : 0)+(((p)->y < (r)->y_min) ? BOT : 0)+(((p)->y > (r)->y_max) ? TOP : 0))
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
struct point {double x, y;};
struct rect {double x_min, y_min, x_max, y_max;};
int cohen_sutherland (const struct rect *r, struct point *a, struct point *b){
int code_a, code_b, code;
struct point *c;
code_a = vcode(r, a);
code_b = vcode(r, b);
while (code_a | code_b) {
if (code_a & code_b)return -1;
if (code_a){
code = code_a;
c = a;
} else {
code = code_b;
c = b;
}
if (code & LEFT) {
c->y += (a->y - b->y) * (r->x_min - c->x) / (a->x - b->x);
c->x = r->x_min;
} else if (code & RIGHT) {
c->y += (a->y - b->y) * (r->x_max - c->x) / (a->x - b->x);
c->x = r->x_max;
}
else if (code & BOT) {
c->x += (a->x - b->x) * (r->y_min - c->y) / (a->y - b->y);
c->y = r->y_min;
} else if (code & TOP) {
c->x += (a->x - b->x) * (r->y_max - c->y) / (a->y - b->y);
c->y = r->y_max;
}
if (code == code_a) {
a = c;
code_a = vcode(r,a);
} else {
b = c;
code_b = vcode(r,b);
}
}
return 0;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->Repaint();
const int m = 6;
int n = 5;
double EndXmas[m]={225,275,300,250,200,225};
double EndYmas[m]={300,300,250,200,250,300};
const rect r1[100]={200,260,250,290};
const rect r2[100]={260,210,290,290};
const rect r3[100]={200,210,250,240};
if (RadioButton2->Checked){
for (int i=0, j=1; i<n; i++,j++){
Canvas->Pen->Color=(TColor)RGB(255,0,0);
Canvas->Pen->Width=2;
Canvas->MoveTo(EndXmas[i],EndYmas[i]);
Canvas->LineTo(EndXmas[j],EndYmas[j]);
}
}
int x1,x2,x3;
double xa;
double ya;
double xb;
double yb;
for (int i=0, j=1; i<n; i++,j++){
point a[100]={EndXmas[i],EndYmas[i]};
point b[100]={EndXmas[j],EndYmas[j]};
x1 = cohen_sutherland (r1, a, b);
x2 = cohen_sutherland (r2, a, b);
x3 = cohen_sutherland (r3, a, b);
if ((x1==0)||(x2==0)||(x3==0)){
xa = a->x;
ya = a->y;
xb = b->x;
yb = b->y;
}
Canvas->Pen->Color=(TColor)RGB(255,0,0);
if (RadioButton2->Checked) Canvas->Pen->Color=(TColor)RGB(225,225,225);
Canvas->Pen->Width=2;
Canvas->MoveTo(xa,ya);
Canvas->LineTo(xb,yb);
}
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Form1->Color = RGB (225, 225, 225);
}