Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C++
<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);
}
Posted
Updated 11-Dec-17 1:21am

1 solution

The posted code is C, not C++.

If you only want to convert the algorithm, create Java classes for the point and rect structs with the required member functions (construction, copy, get, set), and make vcode a function accepting references to those classes.

Or rewrite the code first as C++ with the struct data members being protected so that you have to implement member functions for access. The resulting C++ code should then be convertable to Java without problems.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900