Setting the draggable containment to coordinates [x1, y1, x2, y2] - coordinates

The white div is the parent container while the red div is the child and also the draggable div.
Now I don't want the red div edges to enter the parent div when dragged so I tried to set the draggable containment to x and y coordinates.
parDivLeft = $('div#parent').offset().left;
parDivTop = $('div#parent').offset().top;
parDivWid = $('div#parent').width();
parDivHei = $('div#parent'). height();
dragDivW = $('div#drag').width();
dragDivH = $('div#drag').height();
coorX = dragDivW - parDivWid;
coorY = dragDivH - parDivHei;
x1 = parDivLeft - coorX;
x2 = parDivLeft + parDivWid + coorX;
x1 = parDivTop - coorY;
x2 = parDivTop + parDivHei + coorY;
$('div#drag').draggable({containment:[x1,y1,x2,y2]);
But am not achieving what I want. The red div edges still come into the white div which I don't want. Please can someone help me out

You can create a div element and append it to the body using it as the containment element.
//Using values from your code
newDivH = x2 - x1;
newDivW = y2 - y1;
$newDiv = $('<div>').css({
position:'absolute',
left:y1+'px',
top:x1+'px',
width: newDivW+'px',
height: newDivH+'px',
zIndex:-5
})
$('body').append($newDiv);
$('div#drag').draggable({containment:$newDiv);

Related

Detect user is outside the GoogleMap route (perpendicular distance to path)

I am creating a simple navigation application using GoogleMap.
When the user set the start location and destination location I am drawing the path using polylines.
If the user deviate from the path, I want to redraw the path based on current user location.
Problem I have is how to detect user is not in the current route?
I am using
https://maps.googleapis.com/maps/api/directions/json?
to get the directions.
Is there a function in that to detect whether user is outside the route?
I also have the list of coordinates used to draw the poly lines.
Can they be used to detect user moved outside the current route ?
Based on an example I found in web, I created this method. The example is not designed to for the map coordinates.
Function for finding the distance between a point and an edge in java
Is there a better way of doing this?
static double perpendicularDistance(LatLng point, LatLng start, LatLng end) {
double x = point.longitude;
double y = point.latitude;
double x1 = start.longitude;
double y1 = start.latitude;
double x2 = end.longitude;
double y2 = end.latitude;
double A = x - x1;
double B = y - y1;
double C = x2 - x1;
double D = y2 - y1;
double dot = A * C + B * D;
double len_sq = C * C + D * D;
double param = -1;
if (len_sq != 0) //in case of 0 length line
param = dot / len_sq;
double xx, yy;
if (param < 0) {
xx = x1;
yy = y1;
} else if (param > 1) {
xx = x2;
yy = y2;
} else {
xx = x1 + param * C;
yy = y1 + param * D;
}
var dx = x - xx;
var dy = y - yy;
// one degree is equivalent to 111km approximately
return (sqrt(dx * dx + dy * dy)).abs() * 111000;
}
May be you need to get coordinates of user everytime he/she walks, and check if that coordinates lies in the list of coordinates which you have.
You can get location using https://pub.dev/packages/location.
These is just a thought, I can be wrong.
You can use this package
https://pub.dev/packages/google_maps_utils
with this method to check if you are not near of your route:
/// Computes whether the given point lies on or near a polyline, within a specified
/// tolerance in meters. The polyline is composed of great circle segments if geodesic
/// is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing
/// segment between the first point and the last point is not included.
static bool isLocationOnPathTolerance(Point point, List<Point> polyline,
bool geodesic, double tolerance
)
Where point is, a class to represent two dimensional positions, for example;
var rightBottom = const Point(200, 400);
As you can see, you can send the tolerance for the distance in meters.

Get Y value of line from X pixel value in ChartJS 2

I have a line graph in chartjs, and I want to find the Y value for an arbitrary point on the line given the pixel value from the x axis.
My Graph
Currently I'm hooking into the afterDatasetsDraw event to add that shaded region to the graph, but I also want to find out the values of the black line (Axis B) at the start and end of the shaded region, which don't necessarily line up with my data points.
afterDatasetsDraw: function (chart) {
var options = chart.config.options.plugins.shader;
if (!options.hasOwnProperty('points')) {
return;
}
if (options.points.length < 2) {
return;
}
var ctx = chart.chart.ctx;
var x1, y1, x2, y2, x3, y3, x4, y4, x0, xf;
console.log(chart);
x0 = chart.scales['x-axis-0'].left;
xf = chart.scales['x-axis-0'].right;
x1 = ((xf - x0) * 0.12) + x0; // start shading at 12% in for example
y1 = chart.scales['A'].bottom;
x2 = x1;
y2 = chart.scales['A'].top;
x3 = ((xf - x0) * 0.66) + x0; // end shading at 66% for example
y3 = y2;
x4 = x3;
y4 = y1;
// console.log(chart.scales['B'].getValueForPixel(x1));
// console.log(chart.scales['B'].getValueForPixel(x3));
// console.log(chart.scales['A'].getValueForPixel(x1));
// console.log(chart.scales['A'].getValueForPixel(x3));
// console.log(chart.scales['x-axis-0'].getValueForPixel(x1));
// console.log(chart.scales['x-axis-0'].getValueForPixel(x3));
ctx.fillStyle = 'rgba(127, 127, 127, 0.3)';
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.lineTo(x4, y4);
ctx.lineTo(x1,y1);
ctx.closePath();
ctx.fill();
}
});
I would assume from the docs that I could use the scales getValueForPixel() method (as is shown commented out) but those are returning strange values. The X-axis values are coming back as 1 and 3 which as far as I can tell are the indexes of the closest data point. The Y scales are returning numbers that don't correspond with anything in the dataset or the rendered graph.

How to add a shading pattern to a custom shape

I have drawn an equilateral triangle as follows using iText
canvas.setColorStroke(BaseColor.BLACK);
int x = start.getX();
int y = start.getY();
canvas.moveTo(x,y);
canvas.lineTo(x + side,y);
canvas.lineTo(x + (side/2), (float)(y+(side*Math.sin(convertToRadian(60)))));
canvas.closePathStroke();
I wish to multi color gradient in this shape i.e. fill it with shading comprising of BaseColor.PINK and BaseColor.BLUE. I just can't find a way to do this with iText ?
I've created an example called ShadedFill that fills the triangle you are drawing using a shading pattern that goes from pink to blue as show in the shaded_fill.pdf:
PdfContentByte canvas = writer.getDirectContent();
float x = 36;
float y = 740;
float side = 70;
PdfShading axial = PdfShading.simpleAxial(writer, x, y,
x + side, y, BaseColor.PINK, BaseColor.BLUE);
PdfShadingPattern shading = new PdfShadingPattern(axial);
canvas.setShadingFill(shading);
canvas.moveTo(x,y);
canvas.lineTo(x + side, y);
canvas.lineTo(x + (side / 2), (float)(y + (side * Math.sin(Math.PI / 3))));
canvas.closePathFillStroke();
As you can see, you need to create a PdfShading object. I created an axial shading that varies from pink to blue from the coordinate (x, y) to the coordinate (x + side, y). With this axial shading, you can create a PdfShadingPattern that can be used as a parameter of the setShadingFill() method to set the fill color for the canvas.
See ShadedFill for the full source code.

Get coordinate of new rectangle with Matlab's imrect

For my project, I have a lot of pictures that I need to extract a ROI. When my project starts, I want a picture to show and then the user to select the ROI that he/she wants. The function imrect seems to be doing that. I am trying to get the coordinate of the rectangle, once it has been dragged or resized. The problem is that the values returned do not seem to be correct.
I can't seem to find the problem and the related questions didn't help. I tried using imcrop, but couldn't do any better...
function [ new_image ] = getRoi(image)
rect = size(image);
rect = round(rect ./2);
figure, imshow(image);
h = imrect(gca, [5 5 rect(2) rect(1)]);
addNewPositionCallback(h,#(p) title(mat2str(p,3)));
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
accepted_pos = wait(h);
setPositionConstraintFcn(h,fcn);
%getPositionConstraintFcn(h);
pos = getPosition(h);
if round(pos(1)) < round(pos(2))
X1 = round(pos(1))
X2 = round(pos(2))
else
X1 = round(pos(2))
X2 = round(pos(1))
end
if round(pos(3)) < round(pos(4))
Y1 = round(pos(3))
Y2 = round(pos(4))
else
Y1 = round(pos(4))
Y2 = round(pos(3))
end
new_image = image(Y1:Y2, X1:X2);
%name = strcat('Roi_', datestr(clock, 'yyyymmddTHHMMSS'),'.png');
%prtIm(new_image, name, '-s');
new_image = image;
end
getPosition returns [xmin, ymin, width, height]. To get the coordinates you want, try
X1 = round(pos(1));
Y1 = round(pos(2));
X2 = round(X1 + pos(3));
Y2 = round(Y1 + pos(4));

Creating a circular mask for my graph

I'm plotting a square image, but since my camera views out of a circular construction, I want the image to look circular as well. So to do this, I just wanted to create a mask for the image (basically create a matrix, and multiply my data by the mask, so if I want to retain my image I am multiplying by one, and if I want that part of the image to go to black, I multiply by 0).
I'm not sure the best way to make a matrix that will represent a circular opening though. I just want every element within the circle to be a "1" and every element outside the circle to be a "0" so I can color my image accordingly. I was thinking of doing a for loop, but I was hoping there was a faster way to do it. So...all I need is:
A matrix that is 1280x720
I need a circle that has a diameter of 720, centered in the middle of the 1280x720 matrix (what I mean by this is all elements corresponding to being within the circle have a "1" and all other elements have a "0"
My attempt
mask = zeros(1280,720)
for i = 1:1280
for j = 1:720
if i + j > 640 && i + j < 1360
mask(i,j) = 1;
end
end
end
Well the above obviously doesn't work, I need to look at it a little better to form a better equation for determing when to add a 1 =P but ideally I would like to not use a for loop
Thanks, let me know if anything is unclear!
#kol 's answer looks correct. You can do this with vectorized code using the meshgrid function.
width = 1280;
height = 720;
radius = 360;
centerW = width/2;
centerH = height/2;
[W,H] = meshgrid(1:width,1:height);
mask = ((W-centerW).^2 + (H-centerH).^2) < radius^2;
Here is a possible solution:
width = 160;
height = 120;
mask = zeros(width, height);
center_x = width / 2;
center_y = height / 2;
radius = min(width, height) / 2;
radius2 = radius ^ 2;
for i = 1 : width
for j = 1 : height
dx = i - center_x;
dy = j - center_y;
dx2 = dx ^ 2;
dy2 = dy ^ 2;
mask(i, j) = dx2 + dy2 <= radius2;
end;
end;
picture = randn(width, height); % test image :)
masked_image = picture .* mask;
imagesc(masked_image);