• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 15:48
CET 21:48
KST 05:48
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump0Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3
StarCraft 2
General
Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump Chinese SC2 server to reopen; live all-star event in Hangzhou Maestros of the Game: Live Finals Preview (RO4) BGE Stara Zagora 2026 announced Weekly Cups (Nov 24-30): MaxPax, Clem, herO win
Tourneys
StarCraft2.fi 15th Anniversary Cup Tenacious Turtle Tussle Sparkling Tuna Cup - Weekly Open Tournament RSL Offline Finals Info - Dec 13 and 14! StarCraft Evolution League (SC Evo Biweekly)
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress Mutation # 500 Fright night
Brood War
General
BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ Foreign Brood War Data analysis on 70 million replays MBCGame Torrents
Tourneys
Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL21] RO16 Group D - Sunday 21:00 CET [BSL21] RO16 Group A - Saturday 21:00 CET
Strategy
Current Meta Game Theory for Starcraft How to stay on top of macro? PvZ map balance
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile ZeroSpace Megathread The Perfect Game
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas TL Mafia Community Thread
Community
General
Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine European Politico-economics QA Mega-thread US Politics Mega-thread The Big Programming Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
TL+ Announced Where to ask questions and add stream? The Automated Ban List
Blogs
I decided to write a webnov…
DjKniteX
Physical Exertion During Gam…
TrAiDoS
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1540 users

The Big Programming Thread - Page 661

Forum Index > General Forum
Post a Reply
Prev 1 659 660 661 662 663 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Manit0u
Profile Blog Joined August 2004
Poland17510 Posts
September 03 2015 11:42 GMT
#13201
I'm no good with trigonometry. Could you help me out with Haversine formula a bit?

I have this query to get locations in select radius range from target location. Could it be made simpler? It works but it looks horrible...


$query = $queryBuilder
->select('CAST((2 * ATAN2(SQRT(POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude))), SQRT(1 - POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude)))) * 6371) AS DECIMAL(65,10)) AS d')
->having($expr->lte('d', ':radius'))
->setParameter('radius', $radius)
->getQuery();
Time is precious. Waste it wisely.
mca64Launcher_
Profile Joined June 2015
Poland629 Posts
Last Edited: 2015-09-03 11:46:50
September 03 2015 11:46 GMT
#13202
who cares how it looks. Its works! Its all you need.
Manit0u
Profile Blog Joined August 2004
Poland17510 Posts
September 03 2015 13:12 GMT
#13203
Yeah, but I'm afraid of complicated maths affecting database performance when requests (and thus queries) start to ramp up.
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18143 Posts
Last Edited: 2015-09-03 13:59:08
September 03 2015 13:58 GMT
#13204
On September 03 2015 22:12 Manit0u wrote:
Yeah, but I'm afraid of complicated maths affecting database performance when requests (and thus queries) start to ramp up.

So do it in your PHP instead of SQL. I have no idea what is going to be faster. I would, as a general principal, expect PHP to do trigonometry faster than SQL, although you never knows until you test.
Manit0u
Profile Blog Joined August 2004
Poland17510 Posts
Last Edited: 2015-09-03 14:17:51
September 03 2015 14:17 GMT
#13205
I'd have to grab all rows from db (at least lat and long values for each row) and then do the calculations for each one of them. Sounds simple enough but then you have to do all the circus with having a collection, performing calculations for each element, removing elements that didn't match the criteria and returning the collection.

I need to think about it.
Time is precious. Waste it wisely.
Cyx.
Profile Joined November 2010
Canada806 Posts
September 03 2015 14:29 GMT
#13206
On September 03 2015 22:58 Acrofales wrote:
Show nested quote +
On September 03 2015 22:12 Manit0u wrote:
Yeah, but I'm afraid of complicated maths affecting database performance when requests (and thus queries) start to ramp up.

So do it in your PHP instead of SQL. I have no idea what is going to be faster. I would, as a general principal, expect PHP to do trigonometry faster than SQL, although you never knows until you test.

I feel like I would expect SQL to do it faster - I can't see SQL being *slow* at trig, and I feel like the overhead of selecting the ones you need in PHP would more than outweigh the cost of using SQL trig. I have literally no idea though.
Manit0u
Profile Blog Joined August 2004
Poland17510 Posts
September 03 2015 14:50 GMT
#13207
I'll test performance of SQL computations versus this thing:


public function getOfficesInRadius($latitude, $longitude, $radius)
{
$geolocations = $this->findAll();

foreach ($geolocations as $key => $location) {
$distance = $this->haversineGreatCircleDistance($latitude, $longitude, $location->getLatitude(), $location->getLongitude());

if ($distance > $radius) {
unset($geolocations[$key]);
}
}

return $geolocations;
}

public function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo)
{
// degrees to radians
$latitudeFrom = deg2rad($latitudeFrom);
$latitudeTo = deg2rad($latitudeTo);
$longitudeFrom = deg2rad($longitudeFrom);
$longitudeTo = deg2rad($longitudeTo);

$latitudeDelta = $latitudeTo - $latitudeFrom;
$longitudeDelta = $longitudeTo - $longitudeFrom;

$angle = 2 * asin(sqrt(pow(sin($latitudeDelta / 2), 2) + cos($latitudeFrom) * cos($latitudeTo) * pow(sin($longitudeDelta / 2), 2)));

return $angle * self::EARTH_RADIUS;
}
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18143 Posts
Last Edited: 2015-09-03 15:06:45
September 03 2015 14:56 GMT
#13208
On September 03 2015 23:29 Cyx. wrote:
Show nested quote +
On September 03 2015 22:58 Acrofales wrote:
On September 03 2015 22:12 Manit0u wrote:
Yeah, but I'm afraid of complicated maths affecting database performance when requests (and thus queries) start to ramp up.

So do it in your PHP instead of SQL. I have no idea what is going to be faster. I would, as a general principal, expect PHP to do trigonometry faster than SQL, although you never knows until you test.

I feel like I would expect SQL to do it faster - I can't see SQL being *slow* at trig, and I feel like the overhead of selecting the ones you need in PHP would more than outweigh the cost of using SQL trig. I have literally no idea though.

Yeah, but the exerpt had no where clause, so I (mistakenly) assumed he simply wanted all distances. If there's more to the query then the only way to see speedups is to exhaustively test it.

Other than that, the only way I can think of making that faster is by declaring a MySQL procedure that does the calculation. You should then be able to set a variable to compute:
POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude))

and store it in a local variable. That way you don't have to compute it twice in the query. However, I don't know what the overhead is of doing that kinda stuff in MySQL.


EDIT: I was going to suggest using cosine law instead of haversine if your distances are large enough, but I checked, and float precision in MySQL is actually quite shockingly low. If precision is important, stick with Haversine. If precision ISN'T important, and a rough estimate is okay, then you can use pythagoras for most stuff unless you are computing distances at very high latitudes. Here's an overview of your options (and the formula for calculation): http://www.movable-type.co.uk/scripts/latlong.html
Acrofales
Profile Joined August 2010
Spain18143 Posts
September 03 2015 15:43 GMT
#13209
On September 03 2015 23:50 Manit0u wrote:
I'll test performance of SQL computations versus this thing:


public function getOfficesInRadius($latitude, $longitude, $radius)
{
$geolocations = $this->findAll();

foreach ($geolocations as $key => $location) {
$distance = $this->haversineGreatCircleDistance($latitude, $longitude, $location->getLatitude(), $location->getLongitude());

if ($distance > $radius) {
unset($geolocations[$key]);
}
}

return $geolocations;
}

public function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo)
{
// degrees to radians
$latitudeFrom = deg2rad($latitudeFrom);
$latitudeTo = deg2rad($latitudeTo);
$longitudeFrom = deg2rad($longitudeFrom);
$longitudeTo = deg2rad($longitudeTo);

$latitudeDelta = $latitudeTo - $latitudeFrom;
$longitudeDelta = $longitudeTo - $longitudeFrom;

$angle = 2 * asin(sqrt(pow(sin($latitudeDelta / 2), 2) + cos($latitudeFrom) * cos($latitudeTo) * pow(sin($longitudeDelta / 2), 2)));

return $angle * self::EARTH_RADIUS;
}


By taking the arcsin rather than the arctan you run into problems with precision for small distances (somewhere around a meter according to stackexchange). If that's not an issue, carry on. If it is, use the arctan instead:


$a = pow(sin($latitudeDelta / 2), 2) + cos($latitudeFrom) * cos($latitudeTo) * pow(sin($longitudeDelta / 2), 2);
return 2 * atan2(sqrt($a), sqrt(1 - $a)) * self::EARTH_RADIUS;


Or something.
Manit0u
Profile Blog Joined August 2004
Poland17510 Posts
September 03 2015 18:30 GMT
#13210
On September 03 2015 23:56 Acrofales wrote:
Yeah, but the exerpt had no where clause, so I (mistakenly) assumed he simply wanted all distances. If there's more to the query then the only way to see speedups is to exhaustively test it.


I kind of forgot to explain the query and blindly posted the DQL.

What it's supposed to do is to return all rows that match the criteria (all locations whose distance from target is equal to or less than given radius in km).

Precision isn't super important (it's used to display all shops/offices whatever within a certain radius).

I did some testing and PHP won't be good for that. Even though you can split it into chunks that are more understandable and easier to debug you run into real trouble when you're trying to parse an array containing thousands of objects.
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18143 Posts
Last Edited: 2015-09-03 18:54:27
September 03 2015 18:53 GMT
#13211
On September 04 2015 03:30 Manit0u wrote:
Show nested quote +
On September 03 2015 23:56 Acrofales wrote:
Yeah, but the exerpt had no where clause, so I (mistakenly) assumed he simply wanted all distances. If there's more to the query then the only way to see speedups is to exhaustively test it.


I kind of forgot to explain the query and blindly posted the DQL.

What it's supposed to do is to return all rows that match the criteria (all locations whose distance from target is equal to or less than given radius in km).

Precision isn't super important (it's used to display all shops/offices whatever within a certain radius).

I did some testing and PHP won't be good for that. Even though you can split it into chunks that are more understandable and easier to debug you run into real trouble when you're trying to parse an array containing thousands of objects.

Any chance you can use Google Services and just geofence it? :D

This kind of thing is better done with a dedicated GIS system than an SQL backend.

Anyway, if approximate results is enough, use pythagoras. Way simpler and faster.
Manit0u
Profile Blog Joined August 2004
Poland17510 Posts
September 03 2015 20:31 GMT
#13212
On September 04 2015 03:53 Acrofales wrote:
Show nested quote +
On September 04 2015 03:30 Manit0u wrote:
On September 03 2015 23:56 Acrofales wrote:
Yeah, but the exerpt had no where clause, so I (mistakenly) assumed he simply wanted all distances. If there's more to the query then the only way to see speedups is to exhaustively test it.


I kind of forgot to explain the query and blindly posted the DQL.

What it's supposed to do is to return all rows that match the criteria (all locations whose distance from target is equal to or less than given radius in km).

Precision isn't super important (it's used to display all shops/offices whatever within a certain radius).

I did some testing and PHP won't be good for that. Even though you can split it into chunks that are more understandable and easier to debug you run into real trouble when you're trying to parse an array containing thousands of objects.

Any chance you can use Google Services and just geofence it? :D

This kind of thing is better done with a dedicated GIS system than an SQL backend.

Anyway, if approximate results is enough, use pythagoras. Way simpler and faster.


Ehh... It's just a client who wants to add stores/whatever to the db. Put in the address, grab lat/long from google and people can then search for nearby stores on his website.
Time is precious. Waste it wisely.
delHospital
Profile Blog Joined December 2010
Poland261 Posts
September 03 2015 21:48 GMT
#13213
On September 03 2015 20:42 Manit0u wrote:
I'm no good with trigonometry. Could you help me out with Haversine formula a bit?

I have this query to get locations in select radius range from target location. Could it be made simpler? It works but it looks horrible...

+ Show Spoiler +

$query = $queryBuilder
->select('CAST((2 * ATAN2(SQRT(POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude))), SQRT(1 - POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude)))) * 6371) AS DECIMAL(65,10)) AS d')
->having($expr->lte('d', ':radius'))
->setParameter('radius', $radius)
->getQuery();

Using a WITH clause (or something like that) to assign RADIANS($latitude) and RADIANS($longitude) to variables would probably help a bit. Also, can't you pass these variables in a safer way (that is, without string concatenation)?
Manit0u
Profile Blog Joined August 2004
Poland17510 Posts
September 03 2015 23:30 GMT
#13214
On September 04 2015 06:48 delHospital wrote:
Show nested quote +
On September 03 2015 20:42 Manit0u wrote:
I'm no good with trigonometry. Could you help me out with Haversine formula a bit?

I have this query to get locations in select radius range from target location. Could it be made simpler? It works but it looks horrible...

+ Show Spoiler +

$query = $queryBuilder
->select('CAST((2 * ATAN2(SQRT(POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude))), SQRT(1 - POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude)))) * 6371) AS DECIMAL(65,10)) AS d')
->having($expr->lte('d', ':radius'))
->setParameter('radius', $radius)
->getQuery();

Using a WITH clause (or something like that) to assign RADIANS($latitude) and RADIANS($longitude) to variables would probably help a bit. Also, can't you pass these variables in a safer way (that is, without string concatenation)?


Yeah, I'm going to parametrize that and probably use custom DQL functions for that anyway. The code here is what I took from an old system where they built all their queries like that (concatenate strings and send it as plain SQL). Quite a bit of work before me
Time is precious. Waste it wisely.
Crying
Profile Joined February 2011
Bulgaria778 Posts
Last Edited: 2015-09-04 00:43:28
September 04 2015 00:43 GMT
#13215
Anyone here interested in P NP problem? What's your thought on it?
Determination~ Hard Work Surpass NATURAL GENIUS!
delHospital
Profile Blog Joined December 2010
Poland261 Posts
September 04 2015 02:48 GMT
#13216
On September 04 2015 09:43 Crying wrote:
Anyone here interested in P NP problem? What's your thought on it?

There's a lot of unsolved problems in complexity theory. I wonder how many of them we're going to solve before we invent strong AI to do it for us
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
September 04 2015 02:48 GMT
#13217
--- Nuked ---
ZenithM
Profile Joined February 2011
France15952 Posts
Last Edited: 2015-09-04 04:16:59
September 04 2015 04:09 GMT
#13218
On September 04 2015 09:43 Crying wrote:
Anyone here interested in P NP problem? What's your thought on it?

Haha sounds like you had your first class in complexity theory, we're all excited about that at first. After a while, "P vs NP" loses a bit of its charm, unfortunately. The most likely answer is that P != NP, and there are literally hundred of other "likely" results that have been demonstrated "assuming P != NP". And people are not really researching things that assume "P = NP", let's be honest here :D.
The efforts required to research P vs NP aren't actually worth the rewards. Tentative proofs (all for the != case, of course) are always super long (big fat complicated books) and basically uncheckable. The only exciting thing that could happen in research is if someone actually has an algorithm for reduction, and that it can be checked for correctness. The "almost impossible" thing is if someone actually has a program that does that in reasonable time, then you can think about all the sci-fi/thriller/cataclysm shit you can again!

"P vs NP" being unsolvable is likely due to this classes theory being flawed in the first place. Think about it: it's not really relevant to the real world, where anything longer than cubic is already unmanageable ;D.

If you still are interested, watch the Traveling Salesman. We had a lot of fun with my friends watching that a few years back (we were all computer science graduates obviously, don't watch it with people who don't know CS at all, it's just a bad movie in that case :D).
njt7
Profile Joined August 2012
Sweden769 Posts
Last Edited: 2015-09-04 10:12:20
September 04 2015 09:57 GMT
#13219
Nvm thx, just needed to take a step back and think about it for a sec
"All the casters who flamed me ever for anything."
TMG26
Profile Joined July 2012
Portugal2017 Posts
September 04 2015 13:55 GMT
#13220
On September 04 2015 11:48 Nesserev wrote:

Also, the consequences would be massive if P-TIME = NP-TIME, especially for security.



BRING ON THE CHAOS.
Supporter of the situational Blink Dagger on Storm.
Prev 1 659 660 661 662 663 1032 Next
Please log in or register to reply.
Live Events Refresh
Monday Night Weeklies
17:00
#33
Clem vs MaxPaxLIVE!
SteadfastSC642
TKL 632
IndyStarCraft 287
BRAT_OK 177
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Clem_sc2 1163
SteadfastSC 642
TKL 632
IndyStarCraft 287
BRAT_OK 177
ProTech120
JuggernautJason18
StarCraft: Brood War
Calm 1876
Shuttle 616
Counter-Strike
allub143
Heroes of the Storm
Liquid`Hasu499
Other Games
Grubby6246
Beastyqt745
B2W.Neo439
ArmadaUGS179
KnowMe133
XaKoH 96
QueenE88
Trikslyr62
Mew2King53
ZombieGrub40
Livibee35
NarutO 25
Organizations
Other Games
BasetradeTV116
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 19 non-featured ]
StarCraft 2
• kabyraGe 198
• Reevou 2
• Kozan
• Migwel
• AfreecaTV YouTube
• sooper7s
• intothetv
• IndyKCrew
• LaughNgamezSOOP
StarCraft: Brood War
• Azhi_Dahaki56
• Eskiya23 20
• FirePhoenix17
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• Ler42
League of Legends
• TFBlade1386
Other Games
• imaqtpie1905
• Shiphtur148
Upcoming Events
WardiTV 2025
15h 12m
StarCraft2.fi
19h 12m
PiGosaur Monday
1d 4h
StarCraft2.fi
1d 20h
Tenacious Turtle Tussle
2 days
The PondCast
2 days
WardiTV 2025
2 days
StarCraft2.fi
2 days
WardiTV 2025
3 days
StarCraft2.fi
4 days
[ Show More ]
RSL Revival
4 days
IPSL
4 days
Sziky vs JDConan
RSL Revival
5 days
Classic vs TBD
herO vs Zoun
WardiTV 2025
5 days
IPSL
5 days
Tarson vs DragOn
Wardi Open
6 days
Liquipedia Results

Completed

Acropolis #4 - TS3
RSL Revival: Season 3
Kuram Kup

Ongoing

IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
Slon Tour Season 2
WardiTV 2025
META Madness #9
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
RSL Offline Finals
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.