spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Algorithm: How to find closest element, having coordinates and dimension


Let's say your object's center is (x,y) with width w, height h.

The objects (rectangles, I suppose) in the array has center (xi, yi) and width wi, hi.

Your object will connect to the others either from right-edge, top-edge or bottom-edge whose coordinates are:

R1 - R2: ((x+(w/2)), (y-(h/2))) -
((x+(w/2))), ((y+(h/2)))

T1 - T2: ((x-(w/2)), (y+(h/2))) - ((x+(w/2))),
((y+(h/2)))

B1 - B2: ((x-(w/2)), (y-(h/2))) - ((x+(w/2))),
((y-(h/2)))

Objects in array may have shortest distance starting from their left-edge, top-edge or bottom-edges which are similar

Li1 - Li2: ((xi-(wi/2)), (yi-(hi/2))) -
((xi-(wi/2))), ((yi+(hi/2)))

Ti1 - Ti2: ((xi-(wi/2)), (yi+(hi/2))) -
((xi+(wi/2))), ((yi+(hi/2)))

Bi1 - Bi2: ((xi-(wi/2)), (yi-(hi/2))) -
((xi+(wi/2))), ((yi-(hi/2)))

Then,

distance = infinite;
shortest = null;
for all object in array
    find min distance for
        R2 to [Li1,Li2] line
        R1 to [Li1,Li2] line
        R2 to [Bi1,Bi2] line
        R1 to [Bi1,Bi2] line
        R2 to [Ti1,Ti2] line
        R1 to [Ti1,Ti2] line

        T2 to [Li1,Li2] line
        T1 to [Li1,Li2] line
        T2 to [Bi1,Bi2] line
        T1 to [Bi1,Bi2] line
        T2 to [Ti1,Ti2] line
        T1 to [Ti1,Ti2] line

        B2 to [Li1,Li2] line
        B1 to [Li1,Li2] line
        B2 to [Bi1,Bi2] line
        B1 to [Bi1,Bi2] line
        B2 to [Ti1,Ti2] line
        B1 to [Ti1,Ti2] line

    if minDistance < distance
        distance = minDistance
        shortest = i
end for

You can skip some of these calculations according to the relative position of the objects in the array to your object. For example, if Ti1 < B1, you don't need to calculate [Bi1,Bi2] line parts.

I feel that this solution is very straight-forward&dummy and will take rough criticisms from the mathematicians, but I wanted to try to see the receptions.


Categories : Algorithm

Related to : Algorithm: How to find closest element, having coordinates and dimension
Dividing first element of a column vector of dimension 3x1 with column vector of dimension 5x1 and so on in R
You could use outer c(outer(a,b, FUN="/")) #[1] 0.2500000 0.5000000 0.7500000 0.2000000 0.4000000 0.6000000 0.1666667 #[8] 0.3333333 0.5000000 0.1428571 0.2857143 0.4285714 0.1250000 0.2500000 #[15] 0.3750000 If you want it as fractions library(MASS) fractions(c(outer(a,b, FUN="/"))) #[1] 1/4 1/2 3/4 1/5 2/5 3/5 1/6 1/3 1/2 1/7 2/7 3/7 1/8 1/4 3/8 Or if you want to represent it like this c

Categories : R
How to combine coordinates into one svg element in D3?
To answer the specific question of joining pixels that are adjacent to each other, use a bin method to accumulate touching/adjacent pixels. Sort all the cloud's pixels by their pixel position (x,y). Iterate through each pixels, check against all existing bins(arrays of pixels), and if none are touching any points in any of the bins, create a new bin with that pixel as the first item. If the curr

Categories : Javascript
R: Need to populate a matrix with counts of coordinates (from data frame with list of coordinates, some of which are duplicates)
Here's one approach, using the data.table and Matrix packages: library(data.table) library(Matrix) f <- function(df, nx, ny) { ## Tally up the frequencies dt <- data.table(df, key=c("x", "y")) xyN <- dt[, .N, by=key(dt)] ## Place counts in matrix in their respective i/j x/y row/column as.matrix(with(xyN, sparseMatrix(i=x,j=y,x=N,dims=c(nx,ny)))) } ## Check that it

Categories : R
Is it possible to convert Pointer Coordinates to Map Coordinates (Location / Geocoordinates)?
Why not grab the coordinate information from the pushpin. That's the coordinate you are likely using in your collection. Try something like this: int locToRemove = MapLayer.GetPosition(p); DataLayer.Children.Remove(p);

Categories : C#
astlib coordinates code to astropy coordinates code
This is one way to do it: In [1]: import numpy as np In [2]: from astropy import units as u In [3]: from astropy.coordinates import SkyCoord, FK4, FK5, Galactic In [4]: clra = np.zeros(3) In [5]: cldec = np.zeros(3) In [6]: c1 = SkyCoord(clra * u.deg, cldec * u.deg, frame=FK4) In [7]: c2 = c1.transform_to(FK5(equinox='J2000')) In [8]: c3 = c2.transform_to(Galactic) In [9]: print(c3.l.degr

Categories : Python
Recently Add
why this assembly piece of code do jmp forever
Find out if segment is fully inside of polygon
Algorithm for coloring a hexagon tile map with minimum distance (3) for reoccurring colors
Sort pairs to be more consecutive
To find three unique numbers whose number of occurrence is even
Dealing with duplication between unit and integration tests
reflection and symmetry in back tracking queens
Big O analysis for method with multiple parameters
Divide Huge Array of Numbers in Buckets
Algorithm to find adjacent cells in a matrix
Why this code gives WA for Petersen Graph(codechef)?
Complexity of this prime number search algorithm
How to detect if a file has changed?
Given string x,y and z. Determine if z is a shuffle
Basic decryption for simple encryption algorithm
An efficient way to assign user_ids to huge dataset under certain conditions
What's a more efficient implementation of this puzzle?
Generating prime numbers in poly-time
What if I do not use G transpose in calculating Strongly Connected Components?
Dividing an array into optimum no of equal sum sublists
Counting derangements
How to iterate through all cases when partitioning objects
Algorithm: How to find closest element, having coordinates and dimension
Developing player rankings with ELO
How to transform two set of discrete points ( vectors ) to help plotting them on a common scale
Heap Sort Space Complexity
complex root finding algorithm
Every possible combination algorithm
RSA Cryptosystem - Retrieve m
Heap-like data structure with fast random access?
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.