I am trying to determine the number of steps that I need to take (heuristic cost in my implementation) to get from one node to another on a hexagonal grid. The current method that I use works for conventional matrixes, but it produces false results for my hexagonal grid.
My current method:
private void calculateHeuristicCost(Node node, Node endNode) {
if(node != null) {
node.setHeuristicCost(Math.abs(node.getX() - endNode.getX()) + Math.abs(node.getY() - endNode.getY()));
}
}
Expected:
Distance from 'T':
[ 5][ 4][ 4][ 4][ 4]
[ 5][ 4][ 3][ 3][ 3]
[ 4][ 3][ 2][ 2][ 2]
[ 4][ 3][ 2][ 1][ 1]
[ 3][ 2][ 1][ T][ 1]
Actual:
Distance from 'T':
[ 7][ 6][ 5][ 4][ 5]
[ 6][ 5][ 4][ 3][ 4]
[ 5][ 4][ 3][ 2][ 3]
[ 4][ 3][ 2][ 1][ 2]
[ 3][ 2][ 1][ T][ 1]
Could you please help me find the correct formula to find the distance between 2 nodes?
Related
I have an array like this:
[
{"price": 10},
{"price": 20},
{"price": 30}
]
I want to go over each price item and set it to be negative.
If i do something like this:
.[].price = .[].price * -1
I'll get:
[
{
"price": -10
},
{
"price": -10
},
{
"price": -10
}
]
[
{
"price": -20
},
{
"price": -20
},
{
"price": -20
}
]
[
{
"price": -30
},
{
"price": -30
},
{
"price": -30
}
]
How do i do it properly so at the end it will look like this?
[
{"price": -10},
{"price": -20},
{"price": -30}
]
If the output is to be an array, then consider:
map( .price |= -1 * . )
If the price on output must be negative even if the input price is already negative, then you could replace the expression in parens by:
if .price > 0 then .price |= -1 * . else . end
How about
.[] as $x | { price: ($x.price * -1) }
Here is a working example:
https://jqplay.org/s/VeGHuouLRY
I am using following simple code for 2d array in golang, where APPEND function is resulting in Duplicate values rather than appending.
package main
import "fmt"
func main() {
var n int
fmt.Scanf("%d", &n)
array := [][]int{}
row := make([]int, n)
for _, _ = range row {
for j, _ := range row {
fmt.Scanf("%d", &row[j])
}
fmt.Println("Printing current Row", row)
array = append(array, row)
fmt.Println("Printing curent Array", array)
}
fmt.Println("Final Array", array)
}
But Strangely this are not going unexpectedly.
If suppose i want this thing to happen(input)
2
1 2
3 4
and i run this program i get this in return
2 //Dimension for matrix
1 //Iteration one begins
2
Printing current Row [1 2]
Printing curent Array [[1 2]]
3 //Iteration two begins
4
Printing current Row [3 4]
Printing curent Array [[3 4] [3 4]]
Final Array [[3 4] [3 4]]
I am not getting reason that why APPEND function is resulting in duplicating entries .
There by want to know how to correct this also underlying CONCEPT
This behaviour is occurring because you are not adding a new row slice to your array. Each iteration simply reads into the same row slice overriding previous values. Try below:
func main() {
var n int
fmt.Scanf("%d", &n)
array := [][]int{}
for i := 0; i < n; i++ {
row := make([]int, n) // create a new slice to add next row values
for j, _ := range row {
fmt.Scanf("%d", &row[j])
}
fmt.Println("Printing current Row", row)
array = append(array, row)
fmt.Println("Printing curent Array", array)
}
fmt.Println("Final Array", array)
}
use new slice for each iteration of the first loop, and check for the errors:
you read new data to the old same slice, this is why you have duplicated data.
if you want to use append first create array with zero length and cap n:
array := make([][]int, 0, n)
then change your first loop to: for i := 0; i < n; i++ {
and inside this loop make new slice: row := make([]int, n)
2 way:
using append (without fmt.Scan error checking):
package main
import "fmt"
func main() {
n := 0
fmt.Scan(&n)
slice := make([][]int, 0, n)
for i := 0; i < n; i++ {
row := make([]int, n)
for j, _ := range row {
fmt.Scan(&row[j])
}
slice = append(slice, row)
}
fmt.Println(slice)
}
without using append read to s[i][j] (with error checking):
package main
import "fmt"
func main() {
var n int
if m, err := fmt.Scan(&n); m != 1 {
panic(err)
}
s := make([][]int, n)
for i := 0; i < n; i++ {
s[i] = make([]int, n)
for j := 0; j < n; j++ {
if m, err := fmt.Scan(&s[i][j]); m != 1 {
panic(err)
}
}
}
fmt.Println(s)
}
input:
2
1 2
3 4
output:
[[1 2] [3 4]]
I think this test sample code is clear enough to
show how slice of slice works (with commented output):
package main
import "fmt"
func main() {
s1 := [][]int{}
s2 := []int{1, 2}
s1 = append(s1, s2)
fmt.Println(s1) // [[1 2]]
s2[0], s2[1] = 3, 4
fmt.Println(s1) // [[3 4]]
s1 = append(s1, s2)
fmt.Println(s1) // [[3 4] [3 4]]
s2[0], s2[1] = 30, 40
fmt.Println(s1) // [[30 40] [30 40]]
fmt.Println(len(s2), cap(s2)) // 2 2
s3 := [][]int{
[]int{1, 2},
[]int{3, 4},
s2,
s2,
}
fmt.Println(s3) // [[1 2] [3 4] [30 40] [30 40]]
s2[0] = 100
fmt.Println(s3) // [[1 2] [3 4] [100 40] [100 40]]
}
you created slice of slice s1 like this :
array [0] = row[0], row[1]
array [1] = row[0], row[1]
so when you change row it will seen twice.
I'm trying to draw a line strip with points from user clicks on a QOpenGLWidget. If I put vertices manually and do not update them, it works as expected, but if I update my vector of Vertex, it only draws a line from center to the right center of the widget, regardless of vertices location. This is my updateVertices method (called when a user clicks on the widget):
void CurveGLWidget::updateVertices()
{
m_vao.bind();
m_vbo.bind();
m_vbo.allocate(vertices.size() * sizeof(Vertex));
m_vbo.write(0, &vertices.begin(), vertices.size() * sizeof(Vertex));
m_program.enableAttributeArray("position");
m_program.setAttributeBuffer("position", GL_FLOAT, 0, 2, sizeof(Vertex));
m_program.enableAttributeArray("color");
m_program.setAttributeBuffer("color", GL_FLOAT, sizeof(glm::vec2), 4, sizeof(Vertex));
m_vao.release();
m_vbo.release();
}
at this point m_vao and m_vbo are already created.
This is my paintGL method:
void CurveGLWidget::paintGL()
{
if (vertices.size() < 2) {
return;
}
updateVertices();
glClearColor(m_clearColor.r, m_clearColor.g, m_clearColor.b, m_clearColor.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
m_program.bind();
m_vao.bind();
glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
m_vao.release();
m_program.release();
}
this is a sample of user clicks:
V X Y
Vertex[ 0 ] = [ -0.715, 0.48 ]
Vertex[ 1 ] = [ -0.5175, 0.08 ]
Vertex[ 2 ] = [ -0.285, 0.426667 ]
Vertex[ 3 ] = [ -0.2, -0.153333 ]
Vertex[ 4 ] = [ -0.02, 0.64 ]
Vertex[ 5 ] = [ 0.1425, 0.05 ]
Vertex[ 6 ] = [ 0.2875, 0.696667 ]
Vertex[ 7 ] = [ 0.41, 0.03 ]
Vertex[ 8 ] = [ -0.485, -0.396667 ]
Vertex[ 9 ] = [ -0.015, -0.37 ]
Vertex[ 10 ] = [ 0.14, 0.316667 ]
Can anyone tell me what is wrong with this code? How can I update my vertices on demand?
The full CurveGLWidget source code can be found here.
I figured it out. Instead of &vertices.begin() I must use &vertices.front().
I am using rickshaw graph to visualize my time series.
In the tutorial, we need data pair (x, y), where y has to be type double or int to plot like:
var data = [ { x: -1893456000, y: 92228531 }, { x: -1577923200, y: 106021568 }, { x: -1262304000, y: 123202660 } ];
is there anyway I can put string at y, like
var data = [ { x: -1893456000, y: "ID12" }, { x: -1577923200, y: "ID10" }, { x: -1262304000, y: "ID8" } ];
because I want to use the example http://code.shutterstock.com/rickshaw/examples/hover.html
to show the timeseries of IDs when mouse over.
Thanks
I dont think it works this way. But what you can do is, use a numeric Y value (eg 12 for ID12) and use a yFormatter in your hoverDetail:
var hoverDetail = new Rickshaw.Graph.HoverDetail({graph: graph,
xFormatter: function(x) {return x},
yFormatter: function(y) {return "ID"+y}
});
I think this should do the Trick
I have two maps in Groovy [a: 1, b: 2] and [b:1, c:3] and would like to create from them a third map [a: 1, b: 3, c: 3]. Is there a Groovy command that does that?
Edit: Notice that the values in the third map, are a sum of the values from the first two maps, if the keys are identical.
Thanks
Yet another solution would be:
def m1 = [ a:1, b:2 ]
def m2 = [ b:1, c:3 ]
def newMap = [m1,m2]*.keySet().flatten().unique().collectEntries {
[ (it): [m1,m2]*.get( it ).findAll().sum() ]
}
Taking epidemian's answer as inspiriation, you can also write a method to handle multiple maps
def m1 = [a: 1, b: 2]
def m2 = [b: 1, c: 3]
def combine( Map... m ) {
m.collectMany { it.entrySet() }.inject( [:] ) { result, e ->
result << [ (e.key):e.value + ( result[ e.key ] ?: 0 ) ]
}
}
def newMap = combine( m1, m2 )
This should work for any number of maps:
def maps = [[a: 1, b: 2], [b:1, c:3]]
def result = [:].withDefault{0}
maps.collectMany{ it.entrySet() }.each{ result[it.key] += it.value }
assert result == [a: 1, b: 3, c: 3]
The maps.collectMany{ it.entrySet() } expression returns a list of map entries, like [a=1, b=2, b=1, c=3], and then each of those is added to the result.
Another option, if you'd like to keep all the transformation into one expression and make it "more functional", is to first group the entries by key and then sum the values, but I think it's less readable:
def result = maps.collectMany{ it.entrySet() }
.groupBy{ it.key }
.collectEntries{[it.key, it.value.sum{ it.value }]}
The groupBy part returns a map of the form [a:[a=1], b:[b=2, b=1], c:[c=3]] and then the collectEntries transforms that map into another one that has the same kays but has the sum of the lists in the values instead.
The following example demonstrates adding two maps, to a third map m3:
Map m1 = [ a:1, b:2 ];
Map m2 = [ b:1, c:3 ];
Map m3 = [:];
m3 << m1
m3 << m2
A nice way is it use the spread operator:
def m1 = [ a:1, b:2 ]
def m2 = [ b:1, c:3 ]
def m3 = [ *:m1, *:m2 ]
println "m1 = $m1"
println "m2 = $m2"
println "m3 = $m3"
*Credit:http://mrhaki.blogspot.ch/2009/09/groovy-goodness-spread-operator.html
The code above does not work in groovysh but it is fine in the groovy console. (for version 1.8)
I don't think there's a ready-made method for this, maybe use something like:
def m1 = [a: 1, b: 2]
def m2 = [b: 1, c: 3]
def newMap = (m1.keySet() + m2.keySet()).inject([:]) {
acc, k -> acc[k] = (m1[k] ?: 0) + (m2[k] ?: 0); acc
}
This does it:
Map additionJoin( Map map1, Map map2 )
{
def result = [:];
result.putAll( map1 );
result.putAll( map2 );
result.each { key, value ->
if( map1[key] && map2[key] )
{
result[key] = map1[key] + map2[key]
}
}
return result;
}
def a = [a: 1, b: 2]
def b = [b:1,c:3]
def c = additionJoin( a, b )
println c
def merge(map1, map2) {
def add = { map, entry -> map << entry }
map2.inject(map1.inject([:], add), add)
}