• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 18:34
CEST 00:34
KST 07:34
  • 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
[ASL22] Ro4 Preview: Mirror Mirror6[ASL22] Ro8 Preview: Within Reach5[ASL22] Ro8 Preview: In A Tizzy11[ASL22] Ro16 Preview: Holy Diver5[ASL22] Ro16 Preview: Rough Waters10
Community News
Weekly Cups (October 5-11): Farewell to 5.0.162Rollback Netcode in Brood War895.0.17 Patch Notes - October 7, 202624Weekly Cups Results (Sep 28-Oct 4)0SC4ALL: II SC2 Complete Invited Player Lineup10
StarCraft 2
General
SCII needs to put more weight on strategy and tact Server Blocker How do you feel about the mass reverts in the 5.0.17 PTR? 5.0.17 Patch Notes - October 7, 2026 ESL Pro Tour for SC2 discontinued
Tourneys
$3,500 WardiTV Not-Summer Championship Stellar Fest TWO the Moon (Dec 16-20) ISSL (IntoTheTV x SOOP SC2 League): Premier RSL 2026 Offline Finals LCQ Sign-Ups ScienceCraft (October 24-25) - Live Event
Strategy
[H] ZvP Mid-Late Game: Stalkers Collossi HT
Custom Maps
[G] How to playtest arcade maps [A] Nemrods 1/4 players [M] (2) Sweltering Sands [M] (2) Frigid Storage
External Content
The PondCast: SC2 News & Results Mutation # 547 Temple Of Terror Mutation # 546 Catch the Train Mutation # 545 And Drops and Rifts
Brood War
General
[Megathread] Daily Funny Videos BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ ASL FINALS HYPE VIDEO 2X Rollback Netcode in Brood War
Tourneys
[Megathread] Daily Proleagues ASL 22 Finals [ASL22] Semifinal B [ASL22] Semifinal A
Strategy
Cliff Jump Revisited (1 in a 1000 strategy) Simple Questions, Simple Answers Replay Review Process - What do you do?
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Dawn of War IV General RTS Discussion Thread Warcraft III: The Frozen Throne
Dota 2
Dota 2 Champions League Season 3 Begins April 25!
League of Legends
[TL LoL EUW IHs] Teemo shall perish
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Artificial Intelligence Thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Canadian Politics Mega-thread
Fan Clubs
Serral Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Diablo Animated Series on Netflix
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 MLB/Baseball 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Recent Gifted Posts
Blogs
The Link Between Gaming and …
TrAiDoS
[ASL22] Ro4 Day1 Ticket Giv…
bITt.mAN
38 yo Retired SWE loo…
PurE)Rabbit-SF
Can Bots Beat Pros?? Starcr…
namkraft
[meme] I finally understa…
LUCKY_NOOB
Customize Sidebar...

Website Feedback

Closed Threads



Active: 14841 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
Poland17851 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
Poland17851 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
Spain18464 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
Poland17851 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
Poland17851 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
Spain18464 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
Spain18464 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
Poland17851 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
Spain18464 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
Poland17851 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
Poland17851 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
Next event in 11h 26m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Nina 137
ViBE129
NeuroSwarm 120
StarCraft: Brood War
Artosis 433
White-Ra 165
League of Legends
Doublelift5641
Grubby3808
Super Smash Bros
hungrybox565
AZ_Axe42
Other Games
summit1g5211
Liquid`RaSZi1896
FrodaN562
shahzam473
WinterStarcraft470
C9.Mang0231
PiGStarcraft219
ToD212
Livibee143
ArmadaUGS49
ZombieGrub36
Organizations
StarCraft 2
BasetradeTV169
[ Show 15 non-featured ]
StarCraft 2
• musti20045 16
• davetesta5
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• Migwel
• niya90s 0
StarCraft: Brood War
• blackmanpl 32
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis5929
Other Games
• imaqtpie1039
• Shiphtur462
Upcoming Events
The PondCast
11h 26m
WardiTV Qualifier
12h 26m
Online Event
1d 12h
Korean StarCraft League
2 days
Afreeca Starleague
2 days
Rush vs Soulkey
CranKy Ducklings
2 days
AI Arena Tournament
2 days
BSL Open Qualifier
2 days
Sparkling Tuna Cup
3 days
IPSL
3 days
[ Show More ]
BSL Open Qualifier
3 days
WardiTV Weekly
4 days
IntoTheTV X SOOP
5 days
PiGosaur Cup
6 days
The PondCast
6 days
Kung Fu Cup
6 days
Liquipedia Results

Completed

Proleague 2026-10-13
Blizzard Classic Cup 2026
Copium Cup

Ongoing

ASL Season 22
Super Anchor Qualifying S3
Acropolis #5
HCC Season 3
Thunderpick World Champ. '26
ESL Pro League Season 24
Stake Ranked Episode 4
1win Private Club #1
Logitech G Play Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #3
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026

Upcoming

Acropolis #5 - GSC
BSL Season 23
SC4ALL II: Brood War
BSL 23: Non-Korean Championship
HSC XXX
Stellar Fest 2: Lunar Cup
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
eXTREMESLAND 2026
PGL Major Singapore 2026
Stake Ranked Episode 6
BLAST Rivals Fall 2026
IEM Beijing 2026
Stake Ranked Episode 5
PGL Masters Bucharest 2026
1win Private Club #2
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 © 2026 TLnet. All Rights Reserved.