Convert point from coordinate system screen to x y - system

Convert point from coordinate system to java ,
in Java Frame
+-------x
|
|
|
y
I want it as this one
y
|
|
+------x
please help me

You can change it, let y = Ymax - y
public void paintControl(PaintEvent e) {
Canvas canvas = (Canvas) e.widget;
int maxX = canvas.getSize().x - 20;
int maxY = canvas.getSize().y - 20;
e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE));
int h = 0;
int pixelCount = 4;
e.gc.drawLine(50, maxY, maxX, maxY);
e.gc.drawLine(50, maxY, 50, 0 + 20);

Related

OpenCL kernel function crash

I have written a code in OpenCL in which I am not using local (shared) memory. My code crashes during execution and gives error -5. The error goes away when I replace global memory access to cvt_img buffer (in the middle of the code) with some constant values.
I do not understand why this happens, becuase I prevent accessing to out-of-the-scope memory locations using an if statement.
This code is part of a 3D pipeline, but right now, I have seperated it from my main application, and have put it in a seperate project in which all of the buffers are initialized randomly.
The size of the grid (in terms of number of threads) is the same as size of the image (img_size.x, img_size.y) and size of the block is (16, 16). The application is running for 15 images.
void compute_cost_volume(
global float3 *cvt_img,
global float8 *spixl_map,
global float *disp_level,
global int *view_subset,
global int *subset_num,
int array_width, int2 map_size,
int2 img_size, float bl_ratio,
int sp_size, int num_disp, float2 step,
int x, int y, int z, int view_count
)
{
barrier(CLK_GLOBAL_MEM_FENCE);
int idx = map_size.x * map_size.y * z + map_size.x * y + x;
float8 spixl = spixl_map[idx];
float2 center = spixl.s12;
int2 camIdx = (int2)(z % array_width, z / array_width);
float cost_est = 1000000.0, disp_est = 0.0;
for (int dl = 0 ; dl < num_disp ; dl++)
{
float d = disp_level[dl];
float min_val = 1000000.0;
for (int n = 0 ; n < subset_num[z] ; n++)
{
int view = view_subset[n];
int2 viewIdx = (int2)(view % array_width, view / array_width);
float val = 0.0;
for (int i = -2 ; i <= 2 ; i++) for (int j = -2 ; j <= 2 ; j++)
{
//int2 xy_ref = (int2)(center.x - 2*step.x + i*step.x, center.y - 2*step.y + j*step.y);
int2 xy_ref = (int2)(center.x + i*step.x, center.y + j*step.y);
int2 xy_proj = (int2)((int)(xy_ref.x - d*(viewIdx.x - camIdx.x)), (int)(xy_ref.y - bl_ratio*d*(viewIdx.y - camIdx.y) ) );
if (xy_ref.x >= 0 && xy_ref.y >= 0 && xy_proj.x >= 0 && xy_proj.y >= 0 && xy_ref.x < img_size.x && xy_ref.y < img_size.y && xy_proj.x < img_size.x && xy_proj.y < img_size.y)
{
float3 color_ref = cvt_img[img_size.x*img_size.y*z + img_size.x*xy_ref.y + xy_ref.x];
float3 color_proj = cvt_img[img_size.x*img_size.y*view + img_size.x*xy_proj.y + xy_proj.x];
val += fabs(color_ref.x - color_proj.x) + fabs(color_ref.y - color_proj.y) + fabs(color_ref.z - color_proj.z);
}
else
val += 30;
}
if (val < min_val)
min_val = val;
}
if (min_val < cost_est)
{
cost_est = min_val;
disp_est = d;
}
}
spixl_map[idx].s7 = disp_est;
}
kernel void initial_depth_estimation(
global float3 *cvt_img,
global float8 *spixl_map,
global float *disp_level,
int array_width, int2 map_size,
int2 img_size, float bl_ratio,
int sp_size, int disp_num,
global int *view_subset, global int *subset_num
)
{
int x = get_global_id(0);
int y = get_global_id(1);
if (x >= map_size.x || y >= map_size.y)
return;
//float2 step = (float2)(1, 1);
for (int z = 0 ; z < 15 ; z++){
int idx = map_size.x*map_size.y*z + map_size.x*y + x;
// Set The Bounding Box
float2 step = (float2)(1.0, 1.0);
compute_cost_volume(cvt_img, spixl_map, disp_level, view_subset, subset_num,
array_width, map_size, img_size, bl_ratio, sp_size, disp_num, step, x, y, z, 15);
barrier(CLK_LOCAL_MEM_FENCE);
}
}
From the documentation
https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/vectorDataTypes.html
" The vector data type is defined with the type name i.e. char, uchar, short, ushort, int, uint, float, long, and ulong followed by a literal value n that defines the number of elements in the vector. Supported values of n are 2, 4, 8, and 16. "
Therefore, there is no float3, maybe you can try to use float4 and make the last element zero?
Also, assuming that float3 existed, this line of code
float3 color_proj = cvt_img[img_size.x*img_size.y*view + img_size.x*xy_proj.y + xy_proj.x];
does not do what you want, this will produce ONE value that cannot be assigned to vector, you should have used something like
float3 color_proj = (float3) cvt_img[img_size.x*img_size.y*view + img_size.x*xy_proj.y + xy_proj.x];
this would copy the one value returned by the cvt_img[...] to 3 vector elements.

how to display random stuff without overlaping

So I am creating a game in java using awt library.
I have created Meteoroid which will fall from top.
Problem: problem is that Meteoroid x position overlap on some of the other meteroids.
is there math formula to do this?
Random r = new Random();
int maxX = WINDOW_WIDTH;
int minX = 0;
for (int i = 0; i < 100; i++) {
// (x, y, width, height)
Meteoroid m = new Meteoroid(r.nextInt(maxX - minX + 1) + minX, r.nextInt(10)+0, 40, 40);
}
set a value for x position and increment every time a picture is created.

What is the most optimized way of creating a ray tracer?

Currently, I am working with a ray tracer that takes an iterative approach towards developing the scenes. My goal is to turn it into a recursive ray tracer.
At the moment, I have a ray tracer defined to do the following operation to create the bitmap it is stored in:
int WIDTH = 640;
int HEIGHT = 640;
BMP Image(WIDTH, HEIGHT); // create new bitmap
// Slightly shoot rays left of right camera direction
double xAMT, yAMT;
*/
Color blue(0.1, 0.61, 0.76, 0);
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
if (WIDTH > HEIGHT) {
xAMT = ((x + 0.5) / WIDTH) * aspectRatio - (((WIDTH - HEIGHT) / (double)HEIGHT) / 2);
yAMT = ((HEIGHT - y) + 0.5) / HEIGHT;
}
else if (HEIGHT > WIDTH) {
xAMT = (x + 0.5) / WIDTH;
yAMT = (((HEIGHT - y) + 0.5) / HEIGHT) / aspectRatio - (((HEIGHT - WIDTH) / (double)WIDTH) / 2);
}
else {
xAMT = (x + 0.5) / WIDTH;
yAMT = ((HEIGHT - y) + 0.5) / HEIGHT;
}
..... // calculate intersections, shading, reflectiveness.... etc
Image.setPixel(x, y, blue); // this is here just as an example
}
}
Is there another approach to calculating the reflective and refractive child rays outside the double for-loop?
Are the for-loops necessary? // yes because of the bitmap?
What approaches can be taken to minimize/optimize an iterative ray tracer?

How to create an own struct and constants for this struct?

I want to create a struct which is like a CGPoint, but with 3 coordinates instead of 2.
I create it in the following way:
typedef struct {CGFloat x;CGFloat y;CGFloat z;} CG3Vector;
CG_INLINE CG3Vector CG3VectorMake(CGFloat x, CGFloat y, CGFloat z)
{
CG3Vector p; p.x = x; p.y = y; p.z = z; return p;
}
It works fine. But I now want to improve this struct so that it has the constants like for CGPoint: CGPointZero
Also what is the way to introduce the limits for particular components of the struct, like it is for the CGSize, where components are never lower than 0?
Thanks.
You could create constants like this:
const CG3Vector CG3VectorZero = { 0, 0, 0 };
If you want limits, I suppose you can do some checking like this:
CG_INLINE CG3Vector CG3VectorMake(CGFloat x, CGFloat y, CGFloat z)
{
// normalize the values
x = fmod(x, 360);
y = fmod(y, 360);
z = fmod(z, 360);
x = (x < 0) ? 360 + x : x;
y = (y < 0) ? 360 + y : y;
z = (z < 0) ? 360 + z : z;
return (CG3Vector) { x, y, z };
}

Angle between two lines is wrong

I want to get angles between two line.
So I used this code.
int posX = (ScreenWidth) >> 1;
int posY = (ScreenHeight) >> 1;
double radians, degrees;
radians = atan2f( y - posY , x - posX);
degrees = -CC_RADIANS_TO_DEGREES(radians);
NSLog(#"%f %f",degrees,radians);
But it doesn't work .
The Log is that: 146.309935 -2.553590
What's the matter?
I can't know the reason.
Please help me.
If you simply use
radians = atan2f( y - posY , x - posX);
you'll get the angle with the horizontal line y=posY (blue angle).
You'll need to add M_PI_2 to your radians value to get the correct result.
Here's a function I use. It works great for me...
float cartesianAngle(float x, float y) {
float a = atanf(y / (x ? x : 0.0000001));
if (x > 0 && y > 0) a += 0;
else if (x < 0 && y > 0) a += M_PI;
else if (x < 0 && y < 0) a += M_PI;
else if (x > 0 && y < 0) a += M_PI * 2;
return a;
}
EDIT: After some research I found out you can just use atan2(y,x). Most compiler libraries have this function. You can ignore my function above.
If you have 3 points and want to calculate an angle between them here is a quick and correct way of calculating the right angle value:
double AngleBetweenThreePoints(CGPoint pointA, CGPoint pointB, CGPoint pointC)
{
CGFloat a = pointB.x - pointA.x;
CGFloat b = pointB.y - pointA.y;
CGFloat c = pointB.x - pointC.x;
CGFloat d = pointB.y - pointC.y;
CGFloat atanA = atan2(a, b);
CGFloat atanB = atan2(c, d);
return atanB - atanA;
}
This will work for you if you specify point on one of the lines, intersection point and point on the other line.